0% found this document useful (0 votes)
67 views

Aim: Database Connectivity Source Code:: Advance Java Lab

The document contains source code for three Java programs by Deepika (IT-8315) related to database connectivity and communication between a client and server. The first program creates a database table. The second inserts data into the table. The third retrieves and displays data from the table. Later programs establish a connection between a simple server and client to send and receive strings.

Uploaded by

Deepika Handoo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Aim: Database Connectivity Source Code:: Advance Java Lab

The document contains source code for three Java programs by Deepika (IT-8315) related to database connectivity and communication between a client and server. The first program creates a database table. The second inserts data into the table. The third retrieves and displays data from the table. Later programs establish a connection between a simple server and client to send and receive strings.

Uploaded by

Deepika Handoo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

ADVANCE JAVA LAB

PROGRAM: DEEPIKA (IT 8315)

AIM: DATABASE CONNECTIVITY SOURCE CODE: //CREATE TABLE import java.sql.*; public class CreateTable { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:studentdsn"); Statement s =con.createStatement(); //String SQLCommand = "create table student(sname varchar(20),srollno number(10),class varchar(10),marks number(10))"; s.executeQuery("create table stu (sname varchar,srollno number,class varchar,marks number)"); System.out.println("Table Successfully completed"); con.close(); } catch (Exception e) { System.out.println(e);} } } //INSERT DATA import java.sql.*; import java.io.*;

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

public class InsertData { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:studentdsn"); Statement s =con.createStatement(); PreparedStatement ps=con.prepareStatement("insert into stu values(?,?,?,?)"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Student name : "); ps.setString(1,br.readLine()); System.out.println("Enter Student Rollno : "); ps.setInt(2,Integer.parseInt(br.readLine())); System.out.println("Enter Class of Student : "); ps.setString(3,br.readLine()); System.out.println("Enter Total Marks "); ps.setInt(4,Integer.parseInt(br.readLine())); ps.executeUpdate(); System.out.println("Record Successfully Inserted"); con.close(); } catch (Exception e) { System.out.println(e);} } }

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

//DISPLAY DATA import java.sql.*; import java.io.*; public class DisplayData { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:studentdsn"); Statement s =con.createStatement(); ResultSet rs=s.executeQuery("select * from stu"); System.out.println("Student name"+"\t"+"roll"+"\t"+"Class"+"\t"+"Total Marks"); while(rs.next()) { System.out.println(rs.getString(1)+"\t\t"+rs.getInt(2)+"\t" +rs.getString(3)+"\t"+rs.getInt(4)); } System.out.println("Thanks"); con.close(); } catch (Exception e) { System.out.println(e);} } } OUTPUT:

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

REMARKS: ________

SIGNATURE:_____________

AIM: WAP TO ADD TWO NUMBERS TAKING ARGUMENTS THROUGH COMMAND LINE. SOURCE CODE: import java.io.*; class addargu

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

{ public static void main(String [] args)throws IOException { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int sum=a+b; System.out.println("sum of" +a+" and "+b+" is: "+sum+"."); } } OUTPUT:

REMARKS________ SIGNATURE________ AIM: WAP TO ADD TWO NUMBERS BY USING APPLET. SOURCE CODE: import java.awt.*;

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

import java.applet.*; import java.awt.event.*; /* <applet code="applet3" width=500 height=500> </applet> */ public class applet3 extends Applet implements ActionListener { TextField t1,t2,t3; Button b; public void init() { setBackground(Color.cyan); t1=new TextField(6); t2=new TextField(8); t3=new TextField(8); b=new Button("Sum"); add (t1); add (t2); add (t3); add(b); t1.setText(""); t2.setText(""); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { int x=0,y=0,z=0; String s1,s2,s3; if(ae.getSource()==b) { try { s1=t1.getText();

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

s2=t2.getText(); x=Integer.parseInt(s1); y=Integer.parseInt(s2); z=x+y; s3=String.valueOf(z); t3.setText(s3); } catch(Exception e){} } } }

OUTPUT :

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

REMARKS: ____________

SIGNATURE: __________

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

AIM: SETTING UP COMMUNICATION BETWEEN CLIENT AND SERVER // A SIMPLE SERVER PROGRAM import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) throws IOException { // Register service on port 3128 ServerSocket s = new ServerSocket(3128); Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("U R Reconize by server \n\tHello client "); InputStream in= s1.getInputStream(); DataInputStream dis = new DataInputStream(in); String st = dis.readUTF(); System.out.println(st);

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

// Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } } OUTPUT:

// A SIMPLE CLIENT PROGRAM import java.net.*; import java.io.*;

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

public class SimpleClient { public static void main(String args[]) throws IOException { // Open your connection to a server, at port 3128 Socket s1 = new Socket("192.168.5.85",3128); // Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter data for server"); String line=br.readLine(); OutputStream out = s1.getOutputStream(); DataOutputStream d= new DataOutputStream (out); // Send a string! d.writeUTF(line); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } } OUTPUT:

ADVANCE JAVA LAB


PROGRAM: DEEPIKA (IT 8315)

REMARKS: _________

SIGNATURE:___________

You might also like