透過socket訪問資料庫(轉)
透過socket訪問資料庫[@more@]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
/********************************************
格式化輸出資料庫記錄,出自<>
返回值為格式化的字串
********************************************/
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 +=" ";
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 +=" ";
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+=" ";
}
theResult+=" (" +row+ "rows included) ";
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);
}
}
}
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
/********************************************
格式化輸出資料庫記錄,出自<>
返回值為格式化的字串
********************************************/
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 +=" ";
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 +=" ";
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+=" ";
}
theResult+=" (" +row+ "rows included) ";
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
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);
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10796304/viewspace-952543/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 直接透過ODBC API訪問SQL資料庫 (轉)APISQL資料庫
- 【磐維資料庫】透過python訪問磐維資料庫資料庫Python
- 如何透過holer從外網訪問本地的資料庫?資料庫
- 透過API訪問IE Cache (轉)API
- 用perl訪問mysql資料庫(轉)MySql資料庫
- 遠端資料庫的訪問 (轉)資料庫
- JSP訪問資料庫大全(轉)JS資料庫
- MySql資料庫C++訪問(轉)MySql資料庫C++
- 透過API訪問HDFSAPI
- 如何限定IP訪問Oracle資料庫-轉Oracle資料庫
- Applet直接訪問資料庫 (轉)APP資料庫
- 用JDBC訪問一個資料庫(轉)JDBC資料庫
- 透過等待看資料庫資料庫
- JDBC資料庫訪問JDBC資料庫
- 在Linux下訪問MSSQLServer資料庫 (轉)LinuxSQLServer資料庫
- Struts HOW-TO 系列 之 資料庫訪問 (轉)資料庫
- 資料庫學習:透過作業定時同步兩個資料庫(轉)資料庫
- Oracle資料庫訪問限制繞過漏洞 解決Oracle資料庫
- ubuntu下python通過sqlalchemy庫訪問oracle資料庫UbuntuPythonSQLOracle資料庫
- vnc viewer透過外網訪問,vnc viewer透過外網訪問8個步驟VNCView
- 測了一下 透過 DBCA 透過模板 複製資料庫(資料庫架構及資料)資料庫架構
- 大型網站資料庫及資料訪問最佳化(轉)網站資料庫
- Oracle資料庫訪問控制Oracle資料庫
- 異構資料庫訪問資料庫
- jboss訪問資料庫的問題資料庫
- 如何透過SQLyog分析MySQL資料庫MySql資料庫
- docker 中容器透過 API 互相訪問DockerAPI
- 【.bat】IISExpress配置透過IP訪問程式BATExpress
- Oracle,SqlServer,Access資料庫通用訪問類設計(轉)OracleSQLServer資料庫
- 在Linux下訪問MS SQL Server資料庫(轉)LinuxSQLServer資料庫
- 【資料庫資料恢復】透過資料頁恢復Sql Server資料庫資料的過程資料庫資料恢復SQLServer
- 外網訪問MySQL資料庫MySql資料庫
- Oracle資料庫限制訪問IPOracle資料庫
- C#訪問MySQL資料庫C#MySql資料庫
- C#訪問SQLite資料庫C#SQLite資料庫
- 訪問HyperSQL資料庫的方法SQL資料庫
- 限制特定IP訪問資料庫資料庫
- 資料庫如何處理大資料訪問資料庫大資料