有Sock方面的高手吗? int port=8081;改为 int port=80810;server也改,试试你的代码象jdk1.0时代的,呵呵 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 server 你没赋值呀,应该写上的server的IP地址,呵呵 -------------------------------------------------------------------------------- 改了还是不行:( 我将server和client PORT改成80810以后 怎么启动SERVER的时候出错了? Exception in thread "main" java.lang.NoClassDefFoundError: SimpleServer3 s=new Socket(this.getCodeBase().getHost(),port); void loadsales(){Socket s=null;try{s=new Socket("你的server IP地址",port);//string server 没赋值 port不用改DataInputStream in=new DataInputStream(s.getInputStream());String line; to hexiaofeng(java爱好者): 为什么要改PORT? PORT 的最大值好像是65535(两个字节)。你没学过tcp吗? 看看你server端的policy文件巴,可能有关系。 标题:通过socket访问数据库 发布者:flyfox 发布时间:11月17日 详细信息: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); } } } clientimport javax.swing.*;import java.awt.*;import java.io.*;import java.net.*;import java.awt.event.*;import msgbox;import t_read;class Frame1 extends Frame{ Button b_ok,b_clear,b_name,b_refurbish; List list; TextArea ta1; Panel p1,p2; TextField tf1; Socket socket; public String guestname; public Frame1(String title) { super(title); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); tf1=new TextField(60); p1=new Panel(); p2=new Panel(); b_ok=new Button("OK"); b_clear=new Button("CLEAR"); b_name=new Button("change name"); b_refurbish=new Button("refurbish"); list=new List(); ta1=new TextArea(); add(ta1); add("East",list); p1.add(tf1); p1.add(b_ok); p1.add(b_clear); p1.add(b_name); p1.add(b_refurbish); add("South",p1); socket=socketopen.opensocket("127.0.0.1",3000,500,ta1); if (socket==null) { System.out.println("asdfasdfasdfasdfasdf"); msgbox msb= new msgbox("connect failt");//("Message","connect failt"); msb.resize(400,300); msb.show(); } else { t_read t=new t_read(ta1,socket); t.start(); } b_ok.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { sendto(); } }); b_clear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { tf1.setText(""); } }); } private void sendto() { try { String s; s=tf1.getText(); PrintWriter out=new PrintWriter(socket.getOutputStream(),true); out.println(s); System.out.println(s); tf1.setText(""); } catch(IOException e) { System.out.println(e); } catch(IllegalArgumentException d) { System.out.println("IllegalArgumentException"); } } }class socketopen implements Runnable{ private Socket socket; private String host; private int port; private TextArea tr_txt; /* public void getmessage(TextArea tr) { tr_txt=tr; try { BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); boolean more=true; while(more) { String line=in.readLine(); if (line==null) more=false; tr.appendText(line+"\n"); } } catch(IOException e) { } } */ public static Socket opensocket(String s_host,int i_port,int timeout,TextArea tr) { socketopen c=new socketopen(s_host,i_port); Thread t=new Thread(c); t.start(); try { t.join(timeout); } catch(InterruptedException e) { } return c.getsocket(); } public socketopen(String ahost,int iport) { socket=null; host=ahost; port=iport; } public void run() { try { socket=new Socket(host,port); } catch(IOException exe) { } } } 为什么不能用java来执行wget命令 求助 关于树的遍历问题 java 有 内存释放语句?? oracle blob插入不进去图片 请问怎么能知道某个类在哪个jar包中 Java 读取windows目录动态实现目录树构造 关于身份证数据结构 菜鸟问题,类的导入 请教applet的最简单的程序不能正常显示:( 关于包的问题! 关于treeview+数据库的问题,
改了还是不行:(
我将server和client PORT改成80810以后
怎么启动SERVER的时候出错了?
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleServer3
s=new Socket(this.getCodeBase().getHost(),port);
{
Socket s=null;
try{
s=new Socket("你的server IP地址",port);//string server 没赋值 port不用改
DataInputStream in=new DataInputStream(s.getInputStream());
String line;
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);
}
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import msgbox;
import t_read;class Frame1 extends Frame
{
Button b_ok,b_clear,b_name,b_refurbish;
List list;
TextArea ta1;
Panel p1,p2;
TextField tf1;
Socket socket;
public String guestname;
public Frame1(String title)
{
super(title);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}); tf1=new TextField(60);
p1=new Panel();
p2=new Panel();
b_ok=new Button("OK");
b_clear=new Button("CLEAR");
b_name=new Button("change name");
b_refurbish=new Button("refurbish");
list=new List();
ta1=new TextArea();
add(ta1);
add("East",list);
p1.add(tf1);
p1.add(b_ok);
p1.add(b_clear);
p1.add(b_name);
p1.add(b_refurbish);
add("South",p1);
socket=socketopen.opensocket("127.0.0.1",3000,500,ta1);
if (socket==null)
{
System.out.println("asdfasdfasdfasdfasdf");
msgbox msb= new msgbox("connect failt");//("Message","connect failt");
msb.resize(400,300);
msb.show();
}
else
{
t_read t=new t_read(ta1,socket);
t.start();
}
b_ok.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendto();
}
}); b_clear.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText("");
}
});
}
private void sendto()
{
try
{
String s;
s=tf1.getText();
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
out.println(s);
System.out.println(s);
tf1.setText("");
}
catch(IOException e)
{
System.out.println(e); }
catch(IllegalArgumentException d)
{
System.out.println("IllegalArgumentException"); } }
}
class socketopen implements Runnable
{
private Socket socket;
private String host;
private int port;
private TextArea tr_txt;
/* public void getmessage(TextArea tr)
{
tr_txt=tr;
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
boolean more=true;
while(more)
{
String line=in.readLine();
if (line==null)
more=false;
tr.appendText(line+"\n");
}
}
catch(IOException e)
{
}
}
*/
public static Socket opensocket(String s_host,int i_port,int timeout,TextArea tr)
{
socketopen c=new socketopen(s_host,i_port);
Thread t=new Thread(c);
t.start();
try
{
t.join(timeout);
}
catch(InterruptedException e)
{
}
return c.getsocket();
}
public socketopen(String ahost,int iport)
{
socket=null;
host=ahost;
port=iport;
}
public void run()
{
try
{
socket=new Socket(host,port);
}
catch(IOException exe)
{
}
}
}