Slip13
Q1. Write a JDBC
program to create a Mobile (Model_No, Company_Name, Price, Color) table and
insert a record in it.
import
java.sql.*;
import
java.io.*;
import
javax.sql.*;
class
slip13
{
public static void main(String args[])
{
Connection con;
Statement state;
ResultSet rs;
int ch;
boolean flag=true;
String decision;
int no;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:lab.dsn");
System.out.println("Statement
object created");
do
{
System.out.println("\n");
System.out.println("Menu:");
System.out.println("1.create Table");
System.out.println("2.Insert Record into the Table");
System.out.println("3.Display all the Records from the
Table");
System.out.println("4.Exit");
System.out.println("Enter your
choice: ");
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
state=con.createStatement();
String
query="create table mobile"+"(mno int ,"+" mname
varchar(20),"+" price int,"+" color varchar(20))";
state.executeUpdate(query);
System.out.println("Table
created");
case 2:
System.out.println("Enter mobile Number: ");
int
mno=Integer.parseInt(br.readLine());
System.out.println("Enter mobile Name: ");
String mname=br.readLine();
System.out.println("Enter
price: ");
int
price=Integer.parseInt(br.readLine());
System.out.println("Enter color: ");
String color=br.readLine();
String sql="insert
into mobile values(?,?,?,?)";
PreparedStatement
p=con.prepareStatement(sql);
p.setInt(1,mno);
p.setString(2,mname);
p.setInt(3,price);
p.setString(4,color);
p.executeUpdate();
System.out.println("Record Added");
break;
case 3:
state=con.createStatement();
sql="select * from
mobile";
rs=state.executeQuery(sql);
while(rs.next())
{
System.out.println("\n");
System.out.print("\t"
+rs.getInt(1));
System.out.print("\t" +rs.getString(2));
System.out.print("\t" +rs.getInt(3));
System.out.print("\t" +rs.getString(4));
}
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid Choice");
break;
}
}while(ch!=4);
}catch(Exception e)
{
System.out.println(e);
}
}
}
GossipClient.java
import
java.io.*;
import
java.net.*;
public
class GossipClient
{
public static void main(String[] args) throws
Exception
{
Socket sock = new
Socket("127.0.0.1", 3000);
// reading from
keyboard (keyRead object)
BufferedReader keyRead = new
BufferedReader(new InputStreamReader(System.in));
// sending to client
(pwrite object)
OutputStream ostream =
sock.getOutputStream();
PrintWriter pwrite = new
PrintWriter(ostream, true);
// receiving from
server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new
BufferedReader(new InputStreamReader(istream));
System.out.println("Start the
chitchat, type and press Enter key");
String receiveMessage, sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage =
receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage);
// displaying at DOS prompt
}
}
}
}
GossipServer.java
import java.net.*;
public class GossipServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock = sersock.accept( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}