tip:通过socket访问数据库,分 cl.net, display,sqlserver三个类
client.java
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class client extends applet
{
public textarea chat_txt;
private textfield sql_txt;
private button connect,execute;
private socket soc= null;
private printstream ps= null;
listen listen;
public void init()
{
chat_txt= new textarea();
sql_txt= new textfield(20);
connect= new button("connect");
execute= new button("execute");
execute.disable();
panel pp= new panel();
pp.setlayout(new flowlayout());
pp.add(sql_txt);
pp.add(connect);
pp.add(execute);
add("north",pp);
add("center",chat_txt);
}
public boolean action(event evt,object obj)
{
if(evt.target instanceof button)
{
string label= (string)obj;
if(label.equals("connect"))
{
try{
soc= new socket(.netaddress.getlocalhost(),4700);
ps= new printstream(soc.getoutputstream());
ps.println("hello");
ps.flush();
listen= new listen(this,soc);
listen.start();
}catch(exception e)
{
chat_txt.settext(e.tostring());
disconnect();
}
connect.setlabel("disconnect");
execute.enable();
}else if(label.equals("disconnect"))
disconnect();
else if(label.equals("execute"))
{
if(sql_txt.gettext()!= null)
{
ps.println("find");
ps.flush();
ps.println(sql_txt.gettext());
ps.flush();
}
}
}
return false;
}
public void disconnect()
{
if(soc!= null)
{
try{
listen.stop();
ps.println("quit");
ps.flush();
soc.close();
}catch(exception e){}
finally{
listen.stop();
listen= null;
soc= null;
}
execute.disable();
connect.setlabel("disconnect");
}
}
}
class listen extends thread
{
socket socket= null;
datainputstream dis= null;
client parent= null;
public listen(client p,socket s)
{
parent= p;
socket= s;
try{
dis= new datainputstream(socket.getinputstream());
}catch(exception e){}
}
public void run()
{
string line= null;
while(true)
{
try{
line= dis.readline();
}catch(exception e){}
if(line!= null)
parent.chat_txt.appendtext(line);
}
}
}
display.java
/********************************************
格式化输出数据库记录,出自<<使用java进行sql数据库程序设计>>
返回值为格式化的字符串
********************************************/
import java.sql.*;
class display extends object
{
resultset theresultset;
string theresult;
public void display()
{}
public void setresult(resultset result)
{
theresultset= result;
}
public string getstring()
throws sqlexception,noclassdeffounderror
{
theresult= "";
resultsetmetadata metadata= theresultset.getmetadata();
int colcount = metadata.getcolumncount();
int colsize[]= new int[colcount];
string collabel[]= new string[colcount];
int coltype[]= new int[colcount];
string coltname[]= new string[colcount];
int colprec[]= new int[colcount];
int colscale[]= new int[colcount];
theresult +="\n";
for(int i= 1;i<= colcount;i++)
{
colsize[i-1] = metadata.getcolumndisplaysize(i);
collabel[i-1]= metadata.getcolumnlabel(i);
coltype[i-1] = metadata.getcolumntype(i);
coltname[i-1]= metadata.getcolumntypename(i);
colprec[i-1] = metadata.getprecision(i);
colscale[i-1]= metadata.getscale(i);
if(colsize[i-1]<1+ collabel[i-1].length())
colsize[i-1]= 1+collabel[i-1].length();
theresult+= rightpad(collabel[i-1],colsize[i-1]);
}
theresult +="\n\n";
int row= 0;
while(theresultset.next())
{
row++;
for(int i=1;i<= colcount;i++)
{
string colvalue= theresultset.getstring(i);
if(colvalue== null)
colvalue="";
theresult+= rightpad(colvalue,colsize[i-1]);
}
theresult+="\n";
}
theresult+="\n(" +row+ "rows included)\n";
return theresult;
}
private string rightpad(string s,int len)
{
int curlen= s.length();
if(curlen>len)
return repstring("*",len);
return (s+repstring(" ",(len-curlen)));
}
private string repstring(string s,int times)
{
string result="";
for(int i=0;i
result+= s;
return result;
}
}
sqlserver.java
import java.awt.*;
import java.sql.*;
import java.io.*;
import java.net.*;
import display;
public class sqlserver
{
public static void main(string[] args)
{
system.out.println("waiting for connection");
try{
serversocket session= new serversocket(4700);
handlerequests handler= null;
system.out.println("waiting for connection");
while(true)
{
socket socket= null;
socket= session.accept();
if(socket== null)
continue;
system.out.println("connection made");
handler= new handlerequests(socket);
handler.start();
}
}catch(exception e)
{
system.out.println("客户连接失败"+e);
}
}
}
class handlerequests extends thread
{
private datainputstream in= null;
private printstream out= null;
private socket socket= null;
connection theconnection= null;
statement thestatement= null;
resultset theresultset= null;
display display= null;
public handlerequests(socket s)
throws ioexception
{
socket= s;
in= new datainputstream(socket.getinputstream());
out= new printstream(socket.getoutputstream());
}
public void openconnection()
{
try{
class.forname("sun.jdbc.odbc.jdbcodbcdriver");
if(theconnection!= null)
theconnection.close();
theconnection= drivermanager.getconnection("jdbc:odbc:test","admin","1234");
thestatement= theconnection.createstatement();
display= new display();
}catch(exception e)
{
system.out.println(e);
}
}
public void run()
{
openconnection();
try{
string line= null;
while(true)
{
line = in.readline();
if(line!= null)
line= line.trim();
if(line.equals("find"))
{
line = in.readline();
line= line.trim();
theresultset= thestatement.executequery(line);
display.setresult(theresultset);
out.print(display.getstring());
out.flush();
}
if(line.equals("quit"))
break;
if(line.equals("hello"))
{
out.println("welcome to join us");
out.flush();
}
}
in.close();
out.close();
socket.close();
}catch(exception e)
{
system.out.println(e);
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器