codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  multithreading  masterchief at 00:46 on Saturday, March 03, 2007
 

Hey everyone again, i am having a little problem in getting one of methods in my thread class to work. I will show u below. the one class is using the dbsource class which will be below the other one. What isnt working for me is the processQuery method that is being called upon under the processquery1 in the threads class. Can some try and see what my problem is with it. Thanks!!

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class DatabaseServer extends JFrame {
//Creating member variables of the server
JTextArea display;
ServerSocket server;
Socket connection;
DbSource dbs;
int portnum=0;
DataInputStream input;
DataOutputStream output;

public DatabaseServer(String answer, int answer1) {
//the constructor
super("Multithreaded Server");
display = new JTextArea(20,5);
getContentPane().add(new JScrollPane(display), BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

//connects to the dbsource class to whatever the database was typed in
DbSource dbs= new DbSource(answer);

//this will call the method to setup the socket connections
setupConnection(answer1);
}

public static void main(String args[]){
//Ask the user the data source name and the port number they want to use
String answer= JOptionPane.showInputDialog(null,"Please enter the data source name",JOptionPane.MESSAGE_PROPERTY);
String answer1= JOptionPane.showInputDialog(null,"Please enter desired server socket port number",JOptionPane.MESSAGE_PROPERTY);
int port=Integer.parseInt(answer1);

//which the input from the questions above, we can put that information into the Database constructor
DatabaseServer s = new DatabaseServer(answer, port);
}

public void setupConnection(int answer1){
try{

server = new ServerSocket(answer1,100);

display.setText("Server is waiting for a client to connect....");


while (true){
// wait for a client to connect
connection = server.accept();
display.append("\nA client has connected via " + connection.toString());
// each client gets its own server thread
DataBaseServerThread dst = new DataBaseServerThread(connection,dbs, display);
dst.start();
}

}
catch(Exception e){
// when client terminates, socket connection breaks which causes an exception
display.append("\n\n" + e.toString());
}
}

class DataBaseServerThread extends Thread{
DbSource dbs;
JTextArea ta;
DataInputStream input;
DataOutputStream output;
int portnum;

DataBaseServerThread(Socket s, DbSource db, JTextArea ta1 ){

try{
portnum = s.getPort();
input= new DataInputStream(s.getInputStream());
output = new DataOutputStream(s.getOutputStream());

dbs=db;
ta=ta1;
}
catch(Exception e){
}
}

public void run(){

// typical server behavior...endless loop receiving and responding to
// requests from clients
// Note: now this happens in the thread instead of the main Server class

try{
String write=input.readUTF();
while (true){
ta.append("\nServer " + this.getName() + " waiting for message from client at port " +
portnum);
// readUTF will wait until a message is sent from client
// which will arrive via socket's input stream
ta.append("\nServer " + this.getName() + "received: " + write +
" from client at port " + portnum);

//this does a normal query of a table
if (write.toLowerCase().startsWith("select")){
// boolean success=dbs.processQuery(write,false);
// if (success){
// processquery1();
// }
processquery1(write);
}
//this goes into the update method to change info in the database
else if (write.toLowerCase().startsWith("update")){
processupdate(write);
}
else if(write.toLowerCase().startsWith("show tables with")){
processcommandfield(write);
}
else {
processcommand(write);
}


}
}
catch(Exception e){
// when client terminates, socket is broken and thread catches the exception
ta.append("\nClient at port " + portnum + " has quit");
ta.append("\nServer " + this.getName() + " is terminating ");
}
}

//this will go into the process query method of the dbsource class
//then will be put into xml format
public void processquery1(String query){
try{
String table="";
boolean success = dbs.processQuery(query,false);
if (success){
table="<ResultSet>";
table+=" <MetaData>";
for (int i=1;i<=dbs.getFieldCount();i++){
table+=" <Column>" + dbs.getFieldName(i)+ "</Column>";
}
table+=" </MetaData>";
table+=" <Data>";
while( dbs.nextRecord()) {
table+=" <Row>";
for (int i=1;i<=dbs.getFieldCount();i++){
table+=" <" + dbs.getFieldName(i) + ">"
+ dbs.getField(i)
+ "</" + dbs.getFieldName(i)+ ">";
}
table+=" </Row>";
}
table+=" </Data>";
table+="</ResultSet>";

}
else {
System.out.println("error:"+ dbs.getErrorMessage());
}
output.writeUTF(table);
}
catch(Exception e){
// when client terminates, socket is broken and thread catches the exception
System.out.println(e);
}


}





this is the my other class that the dbs is using:
import java.util.*;
import java.sql.*;


public class DbSource {


//names of all the stuff that is going to be used. They are up here
//because so the different methods can call upon these instances
private Connection con;
private Statement stmt;
private DatabaseMetaData dbMetaData;
private ResultSetMetaData rsmd;
private ResultSet rs;
private String error;
private String[] Database;


//this will connect to the database when it is called upon
public DbSource(String sqlName) {

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:"+sqlName;
con = DriverManager.getConnection(url,"Chris","ChrisPW");
}
catch (Exception e){
error="Error: "+e.toString();
}

}

//Sees if the connected has been established, if it hasnt, it will tell you
//that you didnt
public boolean isConnected(){


try{
if (con.isClosed()==true){


return false;
}
else {

return true;
}

}
catch (Exception e){
error="Error: "+e.toString();
}
return true;
}

//This will process the sql statement that was given and then display
//the query after that
public boolean processQuery(String sqlSelect, boolean sql) {

try{

//this will do forward scrolling
if (sql==false){

stmt = con.createStatement();
rs=stmt.executeQuery(sqlSelect);
return true;
}
else
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sqlSelect);
return true;

}

}
catch (Exception e){

error="error"+e.toString();
rs=null;
return false;


}

}

public int processUpdate(String sqlSelect){
try{
//This will update the table that was selected and tell
//the user what changes where made
stmt= con.createStatement();
int nbr= stmt.executeUpdate(sqlSelect);
return nbr;
}
catch (Exception e){
error="error"+e.toString();
return -1;
}
}

public String[] getTables(){

//This gets all the data of all the different tables that are in
//the database. It will then show to the user what table is which
String[] Table = {"Table"};
int row = 0;

try{
DatabaseMetaData dbMetaData = con.getMetaData();
rs = dbMetaData.getTables(null, null, "%", Table);


while (rs.next()){
row++;

}
String[] Database = new String[row];

rs = dbMetaData.getTables(null, null, "%", Table);
row = 0;

while(rs.next()){

Database[row] = rs.getString(3);
row++;

}
return Database;
}

catch (Exception e){
System.out.println("Error: " + e.toString());
error=e.toString();
return null;
}

}

//This method displays what ever is in the field, that the user is asking
//for and then it will return it
public String getField(int field){

String Fieldname="";

try{

Fieldname=rs.getString(field);
return Fieldname;

}
catch (Exception e){
System.out.println("Error:" +e.toString());
error="Error: "+e.toString();
return null;
}


}

//This displays the name of the column
public String getFieldName(int fname){

String Field = "";

try{

ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();

Field = rsmd.getColumnName(fname);

return Field;
}
catch (Exception e){

error="Error: "+e.toString();
return null;
}

}

//This displays whatever the type of field that it is
public String getFieldType(int ftype){

try{

ResultSetMetaData rsmd = rs.getMetaData();
return rsmd.getColumnTypeName(ftype);
}

catch (Exception e){

error="Error: "+e.toString();
return null;
}

}

//it sees how many fields there are
public int getFieldCount(){

try{
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
return columnCount;
}
catch (Exception e){

error="Error: "+e.toString();
return -1;
}


}



//
//
public boolean nextRecord(){


try{
if (rs.next()){
return true;
}

error="End of file";
return false;

}
catch (Exception e){
error="Error: "+e.toString();
return false;
}

}

public boolean prevRecord(){

try{
if (rs.previous()){
return true;
}
error="End of file";
return false;
}
catch (Exception e){
error="Error: "+e.toString();
return false;
}

}

public boolean firstRecord(){

try{
if (rs.first()){
return true;
}
error="End of file";
return false;
}
catch (Exception e){
error="Error: "+e.toString();
return false;
}

}

public boolean lastRecord(){

try{
if (rs.last()){
return true;
}
error="End of file";
return false;
}
catch (Exception e){
error="Error: "+e.toString();
return false;
}

}

//closes the database
public void close(){
try{
con.close();
}
catch(Exception e){
error="Error: "+e.toString();
}
}

//whatever the error message of the last thing that was put in it, that error
//message will display.
public String getErrorMessage(){
return error;
}

}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007