欢迎光临
我们一直在努力

通过socket访问数据库-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

发布者:flyfox

tip:通过socket访问数据库,分 clinet, 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(inetaddress.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<times;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);
}
}
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 通过socket访问数据库-JSP教程,Java技巧及代码
分享到: 更多 (0)