• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client/server/JDBC

 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am doing a client/server arch, It works when i make the client as the application and make the interface as the applet, how do i do it to make it a pure applet?
server prog.

import java.sql.*;
import java.util.Properties;
import java.io.*;
import java.net.*;
public class CommentsServer extends Thread {
public static final int DEFAULT_PORT = 7777;
protected int port;
protected ServerSocket server;

String username ="combtest";
String password = "combtest";

public static void main (String args[]) {
int port=0;
if (args.length == 1) {
try {
port = Integer.parseInt (args[0]);
} catch (NumberFormatException e) { }
}
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String sourceURL ="jdbc racle:thin:@klm:1521:KLMPMIS";
String user="combtest";
String password="combtest";
}
catch (Exception e) {
System.err.println("Failed to load JDBC driver.");
System.exit (1);
}

new CommentsServer (port);
}
public CommentsServer (int port) {
super ("Comments Server");
if (port == 0)
port = DEFAULT_PORT;
this.port = port;
try {
server = new ServerSocket (port);
} catch (IOException e) {
System.err.println ("Error creating server");
System.exit (1);
}
start();
}
public void run() {
System.out.println ("Server Running");
ThreadGroup connections = new ThreadGroup ("Comment Connections");
connections.setMaxPriority(this.getPriority()-1);
try {
while (true) {
Socket client = server.accept();
System.out.println ("Connection from: " + client.getInetAddress().getHostName());
CommentsConnection c = new CommentsConnection (connections, client);
}
} catch (IOException e) {
System.err.println ("Exception listening");
System.exit (1);
}
System.exit (0);
}
}
class CommentsConnection extends Thread {
static int counter = 0;
protected ObjectInputStream in;
protected PrintWriter out;
public CommentsConnection (ThreadGroup group, Socket client) {
super (group, "Connection " + counter++);
try {
in = new ObjectInputStream (client.getInputStream ());
out = new PrintWriter (client.getOutputStream(), true);
} catch (IOException e) {
try {
client.close();
} catch (IOException e2) { }
System.err.println ("Unable to connect.");
return;
}
start();
}
public void run () {
try {
String mode = (String)in.readObject();
if (mode.equals("insert")) {

String name=(String)in.readObject();

try {
String u="jdbc racle:thin:@klm:1521:KLMPMIS";
Connection con=DriverManager.getConnection(u,"combtest","combtest");
PreparedStatement prep=con.prepareStatement("Insert into TEST values(?)");
prep.setString(1,name);

if(prep.executeUpdate()!=1)
throw new Exception("Bad update");


} catch (Exception e) {
out.println ("Error updating: " + e);
return;
}
} else if (mode.equals("query")) {
try {

Connection con=DriverManager.getConnection("jdbc racle:thin:@klm:1521:KLMPMIS","combtest","combtest");
Statement statement=con.createStatement();

ResultSet result=statement.executeQuery("SELECT * FROM TEST");

out.println("Name");

int nameCol=result.findColumn("NAME");


String name,user,comments;

while(result.next())
{

name=result.getString(nameCol);


out.println(name);
}
statement.close();
con.close();


} catch (Exception e) {
out.println ("Error querying: " + e);
return;
}
} else {
out.println ("Invalid Command: " + mode);
}
} catch (Exception e) {
out.println ("Error reading Stream: " + e);
}
out.close();
}
}
//the client app
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CommentsClient {

TextArea ta;
TextField name;
public static final int DEFAULT_PORT = 7777;
private static final String QueryString = "query";
private static final String InsertString = "insert";
private int port = 0;
private String host = null;
private OutputStream os = null;
public CommentsClient (String host, int port, OutputStream os) {
this.host = host;
this.port = ((port == 0) ? DEFAULT_PORT : port);
this.os = os;
query();
}
public CommentsClient (String host, int port, OutputStream os,
String name) {
this.host = host;
this.port = ((port == 0) ? DEFAULT_PORT : port);
this.os = os;
insert(name);
}
private void query () {
PrintWriter out = new PrintWriter (os, true);
try {
Socket s = new Socket (host, port);
ObjectOutputStream oos = new ObjectOutputStream (s.getOutputStream());
// PrintWriter out=new PrintWriter(s.getOutputStream(),true);

oos.writeObject (QueryString);
oos.flush();
BufferedReader in = new BufferedReader (new InputStreamReader (s.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
out.println (line);
}
out.close();
s.close();
} catch (IOException e) {
out.println ("Error querying." + e);
return;
}
}
private void insert (String name) {
PrintWriter out = new PrintWriter (os, true);
try {
Socket s = new Socket (host, port);
ObjectOutputStream oos = new ObjectOutputStream (s.getOutputStream());
oos.writeObject (InsertString);
oos.writeObject (name);

oos.flush();
BufferedReader in = new BufferedReader (new InputStreamReader (s.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
out.println (line);
}
oos.close();
s.close();
} catch (IOException e) {
out.println ("Error inserting." + e);
return;
}
}


public static void main (String args[]) {
if (args.length == 0) {
CommentsClient cc = new CommentsClient ("localhost", 0, System.out);
} else if (args.length == 1) {
CommentsClient cc = new CommentsClient ("localhost", 0, System.out, args[0]);
}
}


}
/the applet interface
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
public class CommentsApplet extends Applet {
TextArea ta;
TextField name;
TextField user;
TextField comments;
public void init () {
Panel p1 = new Panel(new BorderLayout(10, 10));
Button b1 = new Button ("Query");
p1.add (b1, BorderLayout.SOUTH);
ta = new TextArea ();
ta.setEditable(false);
p1.add (ta, BorderLayout.CENTER);
b1.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
CommentsClient cc = new CommentsClient ("localhost", 0, bao);
ta.setText (bao.toString());
}
} );
add (p1);
Panel p2 = new Panel (new BorderLayout (10, 10));
Button b2 = new Button ("Insert");
p2.add (b2, BorderLayout.SOUTH);
Panel p3 = new Panel();
name = new TextField ("", 10);

p3.add (name);

p2.add (p3, BorderLayout.CENTER);
b2.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
CommentsClient cc = new CommentsClient ("localhost", 0, bao, name.getText());
ta.setText (bao.toString());
}
} );
add (p2);
}
}
//the html file
<html>
<body>
<hr>
<applet
code=CommentsApplet.class
width = 400
height = 400
>
</applet>
<hr>
</body>
</html>

pls help,how do i make the client as an applet
thanks in advance.
Jasbir
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic