|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java也能做一些底层语言开发做的事情(难度很高,不是java顶尖高手是做不来的),会见|数据|数据库公布者:flyfox
Tip:经由过程socket会见数据库,分Clinet,Display,sqlServer三个类
Client.java
importjava.awt.*;
importjava.io.*;
importjava.net.*;
importjava.applet.*;
publicclassClientextendsApplet
{
publicTextAreachat_txt;
privateTextFieldsql_txt;
privateButtonconnect,execute;
privateSocketsoc=null;
privatePrintStreamps=null;
Listenlisten;
publicvoidinit()
{
chat_txt=newTextArea();
sql_txt=newTextField(20);
connect=newButton("Connect");
execute=newButton("Execute");
execute.disable();
Panelpp=newPanel();
pp.setLayout(newFlowLayout());
pp.add(sql_txt);
pp.add(connect);
pp.add(execute);
add("North",pp);
add("Center",chat_txt);
}
publicbooleanaction(Eventevt,Objectobj)
{
if(evt.targetinstanceofButton)
{
Stringlabel=(String)obj;
if(label.equals("Connect"))
{
try{
soc=newSocket(InetAddress.getLocalHost(),4700);
ps=newPrintStream(soc.getOutputStream());
ps.println("HELLO");
ps.flush();
listen=newListen(this,soc);
listen.start();
}catch(Exceptione)
{
chat_txt.setText(e.toString());
disconnect();
}
connect.setLabel("Disconnect");
execute.enable();
}elseif(label.equals("Disconnect"))
disconnect();
elseif(label.equals("Execute"))
{
if(sql_txt.getText()!=null)
{
ps.println("FIND");
ps.flush();
ps.println(sql_txt.getText());
ps.flush();
}
}
}
returnfalse;
}
publicvoiddisconnect()
{
if(soc!=null)
{
try{
listen.stop();
ps.println("QUIT");
ps.flush();
soc.close();
}catch(Exceptione){}
finally{
listen.stop();
listen=null;
soc=null;
}
execute.disable();
connect.setLabel("Disconnect");
}
}
}
classListenextendsThread
{
Socketsocket=null;
DataInputStreamdis=null;
Clientparent=null;
publicListen(Clientp,Sockets)
{
parent=p;
socket=s;
try{
dis=newDataInputStream(socket.getInputStream());
}catch(Exceptione){}
}
publicvoidrun()
{
Stringline=null;
while(true)
{
try{
line=dis.readLine();
}catch(Exceptione){}
if(line!=null)
parent.chat_txt.appendText(line);
}
}
}
Display.java
/********************************************
格局化输入数据库纪录,出自<<利用java举行SQL数据库程序计划>>
前往值为格局化的字符串
********************************************/
importjava.sql.*;
classDisplayextendsObject
{
ResultSettheResultSet;
StringtheResult;
publicvoiddisplay()
{}
publicvoidsetResult(ResultSetresult)
{
theResultSet=result;
}
publicStringgetString()
throwsSQLException,NoClassDefFoundError
{
theResult="";
ResultSetMetaDatametaData=theResultSet.getMetaData();
intcolcount=metaData.getColumnCount();
intcolSize[]=newint[colcount];
StringcolLabel[]=newString[colcount];
intcolType[]=newint[colcount];
StringcolTName[]=newString[colcount];
intcolPrec[]=newint[colcount];
intcolScale[]=newint[colcount];
theResult+="
";
for(inti=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+="
";
introw=0;
while(theResultSet.next())
{
row++;
for(inti=1;i<=colcount;i++)
{
Stringcolvalue=theResultSet.getString(i);
if(colvalue==null)
colvalue="";
theResult+=rightPad(colvalue,colSize[i-1]);
}
theResult+="
";
}
theResult+="
("+row+"rowsincluded)
";
returntheResult;
}
privateStringrightPad(Strings,intlen)
{
intcurlen=s.length();
if(curlen>len)
returnrepString("*",len);
return(s+repString("",(len-curlen)));
}
privateStringrepString(Strings,inttimes)
{
Stringresult="";
for(inti=0;i<times;i++)
result+=s;
returnresult;
}
}
sqlServer.java
importjava.awt.*;
importjava.sql.*;
importjava.io.*;
importjava.net.*;
importDisplay;
publicclasssqlServer
{
publicstaticvoidmain(String[]args)
{
System.out.println("Waitingforconnection");
try{
ServerSocketsession=newServerSocket(4700);
handleRequestshandler=null;
System.out.println("Waitingforconnection");
while(true)
{
Socketsocket=null;
socket=session.accept();
if(socket==null)
continue;
System.out.println("Connectionmade");
handler=newhandleRequests(socket);
handler.start();
}
}catch(Exceptione)
{
System.out.println("客户毗连失利"+e);
}
}
}
classhandleRequestsextendsThread
{
privateDataInputStreamin=null;
privatePrintStreamout=null;
privateSocketsocket=null;
ConnectiontheConnection=null;
StatementtheStatement=null;
ResultSettheResultSet=null;
Displaydisplay=null;
publichandleRequests(Sockets)
throwsIOException
{
socket=s;
in=newDataInputStream(socket.getInputStream());
out=newPrintStream(socket.getOutputStream());
}
publicvoidopenConnection()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
if(theConnection!=null)
theConnection.close();
theConnection=DriverManager.getConnection("jdbc:odbc:test","admin","1234");
theStatement=theConnection.createStatement();
display=newDisplay();
}catch(Exceptione)
{
System.out.println(e);
}
}
publicvoidrun()
{
openConnection();
try{
Stringline=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("Welcometojoinus");
out.flush();
}
}
in.close();
out.close();
socket.close();
}catch(Exceptione)
{
System.out.println(e);
}
}
}
首先第一点:jsp,servlet,javabean这些最基本的,嘿嘿,就算你是高手的话,在大行的企业级应用的话还是需要框架的,一个好的框架确实能构解决许多问题。 |
|