experiment 10 · 2016. 2. 29. · catch(exception e) {system.out.println(e);}}} c:\program...
TRANSCRIPT
Experiment 10
1)
import java.sql.*;
class connectiontest
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
Connection con =DriverManager.getConnection("jdbc:odbc:xyz1");
System.out.println("connection to database created");
}
catch(SQLException e)
{
System.out.println("SQL error");
}
catch(Exception e){
System.out.println("errror");
}
}
}
2) import java.sql.*;
import java.io.*;
public class StudentTable
Advanced Java Programming by Mr. Harish E.Khodke
01
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
//a) Connection To A Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
Statement state = con.createStatement();
System.out.println("Statement Object Created.");
sql = "insert into Table1 values(6, 'XYZ')";
state.executeUpdate(sql);
System.out.println("Entry Inserted.");
//results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
Advanced Java Programming by Mr. Harish E.Khodke
}
2)
import java.sql.*;
import java.io.*;
public class StudentTable
{
public static void main(String args[])
{
try
{
String sql;
//ResultSet results;
Advanced Java Programming by Mr. Harish E.Khodke
//a) Connection To A Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
Statement state = con.createStatement();
System.out.println("Statement Object Created.");
ResultSet results = state.executeQuery("select * from Table1");
//sql = "select * from Table1";
//state.executeUpdate(sql);
//System.out.println("Entry Inserted.");
String text = "" ;
while(results.next())
{
text += results.getInt(1) + "\t" + results.getString(2)+ "\n";
}
System.out.println("\nROLL\tNAME");
System.out.println(text);
//results.close();
state.close();
con.close();
}
Advanced Java Programming by Mr. Harish E.Khodke
catch(Exception e)
{
System.out.println(e);
}
}
}
C:\Program Files\Java\jdk1.5.0\bin>javac StudentTable.java
C:\Program Files\Java\jdk1.5.0\bin>java StudentTable
Driver Loaded
Connection to database created.
Statement Object Created.
ROLL NAME
1 hek
6 XYZ
2 ahk
3) import java.sql.*;
import java.io.*;
public class StudentTable1
{
public static void main(String args[])
{
try
{
Advanced Java Programming by Mr. Harish E.Khodke
String sql;
ResultSet results;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
Statement state = con.createStatement();
System.out.println("Statement Object Created.");
//ResultSet results = state.executeQuery( "update Table1 set name = 'John' where rollno = 2");
sql = "update Table1 set name = 'John' where rollno = 2";
state.executeUpdate(sql);
System.out.println("Entry Updated.");
//results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
Advanced Java Programming by Mr. Harish E.Khodke
}
4)
import java.sql.*;
import java.io.*;
public class StudentTable2
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
//a) Connection To A Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
Advanced Java Programming by Mr. Harish E.Khodke
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
Statement state = con.createStatement();
System.out.println("Statement Object Created.");
sql = "delete * from Table1 where rollno = 4";
state.executeUpdate(sql);
System.out.println("Entry Deleted.");
//results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Advanced Java Programming by Mr. Harish E.Khodke
5)
import java.sql.*;
import java.io.*;
public class StudentTable3
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
//a) Connection To A Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
Statement state = con.createStatement();
System.out.println("Statement Object Created.");
sql = "select rollno, name from Table1";
results = state.executeQuery(sql);
String text = "" ;
while(results.next())
{
text += results.getInt(1) + "\t" + results.getString(2) + "\n";
}
System.out.println("\nROLL\tNAME");
Advanced Java Programming by Mr. Harish E.Khodke
System.out.println(text);
results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Advanced Java Programming by Mr. Harish E.Khodke
Experi.8
import java.awt.*;
import javax.swing.*;
public class URL_GUI extends JFrame
{
Container cn=getContentPane();
Advanced Java Programming by Mr. Harish E.Khodke
public URL_GUI()
{
BorderLayout b1=new BorderLayout();
setLayout(b1);
JTextArea tx=new JTextArea("This is for viewing on html");
cn.add(tx,BorderLayout.CENTER);
}
public static void main(String args[])
{
URL_GUI ug=new URL_GUI();
ug.setTitle("Frame for url");
ug.setSize(100,200);
ug.setVisible(true);
}
}
10.
import java.sql.*;
import java.io.*;
public class Database_delete
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:tycm");
Advanced Java Programming by Mr. Harish E.Khodke
String qry="Delete from studtable where rollno=12";
Statement st=con.createStatement();
st.executeUpdate(qry);
System.out.println("Record delted sucess");
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException es)
{
es.printStackTrace();
}
}
}
10.
import java.sql.*;
import java.io.*;
public class Database_estblish
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Advanced Java Programming by Mr. Harish E.Khodke
Connection con=DriverManager.getConnection("jdbc:odbc:tycm");
PreparedStatement pst=con.prepareStatement("Select * from stud");
ResultSet rs=pst.executeQuery();
System.out.println("connection estab");
rs.next();
System.out.println("roll" +rs.getInt(1));
System.out.println("name" +rs.getString(2));
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException es)
{
es.printStackTrace();
}
}
}
Experiment 11
1) import java.sql.*;
public class EmployeeTableUpdate
{
public static void main(String args[])
{
try
Advanced Java Programming by Mr. Harish E.Khodke
{
String sql;
ResultSet results;
PreparedStatement state;
String text;
//a) Establish Connection To Database.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
//results.close();
//state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Advanced Java Programming by Mr. Harish E.Khodke
2) import java.sql.*;
public class EmployeeTableUpdate1
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
PreparedStatement state;
String text;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Advanced Java Programming by Mr. Harish E.Khodke
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
sql = "insert into Table1 values(?, ?)";
state = con.prepareStatement(sql);
state.setInt(1, 21);
state.setString(2, "XYZ");
state.executeUpdate();
System.out.println("\nRecord Inserted.");
//results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Advanced Java Programming by Mr. Harish E.Khodke
3)
import java.sql.*;
public class EmployeeTableUpdate2
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
PreparedStatement state;
Advanced Java Programming by Mr. Harish E.Khodke
String text;
//a) Establish Connection To Database.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
sql = "select * from Table1 where name = ?";
state = con.prepareStatement(sql);
state.setString(1, "Ramesh");
results = state.executeQuery();
text = "" ;
while(results.next())
{
text += results.getInt(1) + "\t" + results.getString(2) + "\n";
}
System.out.println("\nEntries Related To Ramesh.");
System.out.println("NAME\tAGE");
System.out.println(text);
results.close();
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
Advanced Java Programming by Mr. Harish E.Khodke
}
}
4)
import java.sql.*;
public class EmployeeTableUpdate3
{
public static void main(String args[])
{
try
{
String sql;
ResultSet results;
Advanced Java Programming by Mr. Harish E.Khodke
PreparedStatement state;
String text;
//a) Establish Connection To Database.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:abc";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
sql = "delete * from Table1 where name = ?";
state = con.prepareStatement(sql);
state.setString(1, "ABC");
state.executeUpdate();
System.out.println("\nEntry Deleted.");
System.out.println("\nContents Of Database :");
sql = "select * from Table1";
state = con.prepareStatement(sql);
results = state.executeQuery();
text = "" ;
while(results.next())
{
text += results.getInt(1) + "\t" + results.getString(2) + "\n";
}
System.out.println("NAME\tAGE");
System.out.println(text);
results.close();
state.close();
con.close();
Advanced Java Programming by Mr. Harish E.Khodke
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Experiment 09
9) //server
import java.io.*;
import java.net.*;
public class Server
Advanced Java Programming by Mr. Harish E.Khodke
{
public static void main(String args[]) throws Exception
{
int clientport = 999;
int serverport = 998;
int buffer = 1024;
DatagramSocket ds = new DatagramSocket(serverport);
byte NameByte[] = new byte[buffer];
DatagramPacket p = new DatagramPacket(NameByte, NameByte.length);
ds.receive(p);
String receive = new String(p.getData(), 0, p.getLength());
System.out.println("From Client : " + receive);
String send = "Hello " + receive;
NameByte = send.getBytes();
DatagramPacket p2 = new DatagramPacket(NameByte, NameByte.length, InetAddress.getLocalHost(), clientport);
ds.send(p2);
System.out.println("Sending To The Client : " + send);
ds.close();
}
}
//client
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String args[]) throws Exception
{
Advanced Java Programming by Mr. Harish E.Khodke
int clientport = 999;
int serverport = 998;
int buffer = 1024;
DatagramSocket ds = new DatagramSocket(clientport);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Name : ");
String Name = br.readLine();
byte NameByte[] = new byte[buffer];
NameByte = Name.getBytes();
DatagramPacket p = new DatagramPacket(NameByte, NameByte.length,InetAddress.getLocalHost(), serverport);
ds.send(p);
System.out.println("Name Sent To Server.");
byte NameByte2[] = new byte[buffer];
DatagramPacket p2 = new DatagramPacket(NameByte2, NameByte2.length);
ds.receive(p2);
String receive = new String(p2.getData(), 0, p2.getLength());
System.out.println("From Server : " + receive);
ds.close();
}
}
Advanced Java Programming by Mr. Harish E.Khodke
Advanced Java Programming by Mr. Harish E.Khodke