Java DataBase Delete Record.(JDBC)
import java.sql.*;
public class JavaDBDR
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:EmployeesDSN"; //DSN Name
Connection conn=null;
conn=DriverManager.getConnection(url,"","");
Statement s=conn.createStatement();
s.execute("DELETE FROM EMP WHERE EMPCODE=6"); //EMP DataBase Table Name
s.execute("select * from EMP");
ResultSet rs = s.getResultSet();
if (rs != null)
while (rs.next() )
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getString(4) +" "+ rs.getString(5)+ " "+rs.getString(6) );
}
s.close();
conn.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Java DataBase insert Record.(JDBC)
import java.sql.*;
public class JavaDBIR
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:EmployeesDSN";
Connection conn=null;
conn=DriverManager.getConnection(url,"","");
Statement s=conn.createStatement();
s.execute("insert into EMP values(6,'Kamran',10000,'Gulburg','Faisalabad','0300-6602727')");
s.execute("select * from EMP");
ResultSet rs = s.getResultSet();
if (rs != null)
while (rs.next() )
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getString(4) +" "+ rs.getString(5)+ " "+rs.getString(6) );
}
s.close();
conn.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Java DataBase insert Record With Create New Table.(JDBC)
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/* the next 3 lines are Step 2 method 2 from above - you could use the direct
access method (Step 2 method 1) istead if you wanted */
String dataSourceName = "mdbTEST";
String dbURL = "jdbc:odbc:" + dataSourceName;
Connection con = DriverManager.getConnection(dbURL, "","");
// try and create a java.sql.Statement so we can run queries
Statement s = con.createStatement();
s.execute("create table TEST12345 ( column_name integer )"); // create a table
s.execute("insert into TEST12345 values(1)"); // insert some data into the table
s.execute("select column_name from TEST12345"); // select the data from the table
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
if (rs != null) // if rs == null, then there is no ResultSet to view
while ( rs.next() ) // this will step through our data row-by-row
{
/* the next line will get the first column in our current row's ResultSet
as a String ( getString( columnNumber) ) and output it to the screen */
System.out.println("Data from column_name: " + rs.getString(1) );
}
s.execute("drop table TEST12345");
s.close(); // close the Statement to let the database know we're done with it
con.close(); // close the Connection to let the database know we're done with it
}
catch (Exception err) {
System.out.println("ERROR: " + err);
}
}
}
Java DataBase Update Record.(JDBC)
import java.sql.*;
public class UpdateData
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String s="jdbc:odbc:PersonalDSN";
Connection c=null;
c=DriverManager.getConnection(s,"","");
Statement st=c.createStatement();
String s2="Select * from Students";
st.execute("UPDATE STUDENTS SET FIRSTNAME='ABRAR' where FIRSTNAME='KAHSIF' AND CITY='LHR'");
System.out.println("Record has been updated in database successfully");
ResultSet rs=st.executeQuery(s2);
while(rs.next())
{
String rn=rs.getString("RegNo");
String fn=rs.getString("FirstName");
String ln=rs.getString("LastName");
String ad=rs.getString("Address");
String ct=rs.getString("City");
String cn=rs.getString("ContactNo");
System.out.println(rn+" "+fn+" "+ln+" "+ad+" "+ct+" "+cn);
}
c.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Java DataBase Select Record.(JDBC)
import java.sql.*;
public class SelData
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String s="jdbc:odbc:PersonalDSN";
Connection c=null;
c=DriverManager.getConnection(s,"","");
Statement st=c.createStatement();
String sql="SELECT * FROM STUDENTS order by firstname desc";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
String rn=rs.getString("RegNo");
String fn=rs.getString("FirstName");
String ln=rs.getString("LastName");
String ad=rs.getString("Address");
String ct=rs.getString("City");
String cn=rs.getString("ContactNo");
System.out.println(rn+" "+fn+" "+ln+" "+ad+" "+ct+" "+cn);
}
c.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
JAVA CLIENT AND SERVER Programming Using TCP And UDP
JAVA CLIENT , SERVER Programming Using TCP
//client side
import java.net.*;
import java.io.*;
public class Client{
Client(){
int c=0;
char ch=' ';
try{
Socket ss=new Socket("127.0.0.1",2235);
InputStream is=ss.getInputStream();
Thread.sleep(500);
do{
c=is.read();
ch=(char)c;
System.out.print(ch);
}while(ch!='?');
}catch(Exception e){}
}
public static void main(String as[]){
Client s=new Client();
}
}
// Server side
import java.net.*;
import java.io.*;
public class Server{
Server(){
Socket cli=null;
try{
String msg="Message from Server ?";
byte b[]=msg.getBytes();
ServerSocket ss=new ServerSocket(2235);
System.out.println("Waiting for Client");
cli=ss.accept();
System.out.println("Client Connected "+cli);
OutputStream os=cli.getOutputStream();
os.write(b);
Thread.sleep(1000);
}catch(Exception e){}
}
public static void main(String as[]){
Server s=new Server();
}
}
JAVA CLIENT , SERVER Programming Using UDP
//Client Side
import java.net.*;
import java.io.*;
class DGClient{
DatagramSocket ds=null;
DatagramPacket dp=null;
DGClient(){
try{
ds=new DatagramSocket(2235);
byte b[]=new byte[25];
while(true){
dp=new DatagramPacket(b,b.length);
ds.receive(dp);
System.out.println(new String(dp.getData()));
}
}catch(Exception e){}
}
public static void main(String as[]){
DGClient dg=new DGClient();
}
}
//Server Side
import java.net.*;
import java.io.*;
class DGServer{
DatagramSocket ds=null;
DatagramPacket dp=null;
InetAddress addr=null;
String msg="";
DGServer(){
try{
msg="Message from Server";
addr=InetAddress.getByName("localhost");
ds=new DatagramSocket();
byte b[]=msg.getBytes();
while(true){
dp=new DatagramPacket(b,b.length,addr,2235);
ds.send(dp);
}
}catch(Exception e){}
}
public static void main(String as[]){
DGServer dg=new DGServer();
}
}
Java Chat Server, Client Server
//Server Side
import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}
//Client Side
import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("Hi my server");
sendMessage("How R U Toady");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}
Socket Programming Using GUI,Chat Server With Java Programming In GUI , Chat Room
//Client Side
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ClientApp implements ActionListener,Runnable
{
Frame f;
Socket s;
BufferedReader br;
BufferedWriter bw;
TextField text;
Button button1,button2;
List list;
public static void main(String arg[])
{
new ClientApp();
}
public ClientApp()
{
f=new Frame("Client Application:");
f.setSize(200,300);
f.setLayout(new BorderLayout());
button1=new Button("Send");
button2=new Button("Exit");
button1.addActionListener(this);
button2.addActionListener(this);
list=new List();
text=new TextField();
f.add(list,"Center");
f.add(button1,"West");
f.add(button2,"East");
f.add(text,"South");
f.setVisible(true);
try
{
s=new Socket("localhost",100);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
Thread th;
th=new Thread(this);
th.start();
}catch (Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(button2))
System.exit(0);
else
{
try
{
bw.write(text.getText());
bw.newLine();
bw.flush();
text.setText("");
}catch(Exception m){}
}
}
public void run()
{
try
{
s.setSoTimeout(1);
}catch(Exception e){}
while(true)
{
try
{
list.addItem(br.readLine());
}catch (Exception h){}
}
}
}
//Server Side
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ServerApp implements ActionListener,Runnable
{
Frame f;
ServerSocket s;
Socket s1;
BufferedReader br;
BufferedWriter bw;
TextField text;
Button button1,button2;
List list;
public static void main(String arg[])
{
new ServerApp();
}
public ServerApp()
{
f=new Frame("Server Application:");
f.setSize(200,300);
f.setLayout(new BorderLayout());
button1=new Button("Send");
button2=new Button("Exit");
button1.addActionListener(this);
button2.addActionListener(this);
list=new List();
text=new TextField();
f.add(list,"Center");
f.add(button1,"West");
f.add(button2,"East");
f.add(text,"South");
f.setVisible(true);
try
{
s=new ServerSocket(100);
s1=s.accept();
br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
bw.write("Hello");
bw.newLine();
bw.flush();
Thread th;
th=new Thread(this);
th.start();
}catch (Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(button2))
System.exit(0);
else
{
try
{
bw.write(text.getText());
bw.newLine();
bw.flush();
text.setText("");
}catch(Exception m){}
}
}
public void run()
{
try
{
s1.setSoTimeout(1);
}catch(Exception e){}
while(true)
{
try
{
list.addItem(br.readLine());
}catch (Exception h){}
}
}
}
Java Programming With RMI(Remote Method Invocation)
//Interface
import java.rmi.*;
interface Demo extends Remote{
public int getSqr(int a) throws RemoteException;
}
//Server Side
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class RServer extends UnicastRemoteObject implements Demo{
public RServer() throws Exception{}
{
System.out.println("Server Object Created");
}
public static void main(String as[]){
try{
RServer ser=new RServer();
Naming.rebind("myvar", ser);
}catch(Exception e){}
}
public int getSqr(int a){
return(a*a);
}
}
//Client Side
import java.rmi.*;
import java.rmi.registry.*;
public class RClient{
public static void main(String as[]){
try{
Demo d=(Demo)Naming.lookup("rmi://127.0.0.1/myvar");
int i=d.getSqr(16);
int j=d.getSqr(10);
int k=d.getSqr(4);
System.out.println("The Result is \t"+i);
System.out.println("The Result is \t"+j);
System.out.println("The Result is \t"+k);
}catch(Exception e){}
}
}
2 comments:
each program here is showing 100 error..cn u figure it out ?? :\
Nice job, really good for java novice, than for such wonderful programs
Post a Comment