以下是我照书编写数据库访问中的套接字技术程序
数据库是用Access建的其中有一个表名字是表1。字段有单词,解释。都为
字符型。数据源名为test,用户名为gxy,密码为ookk
运行以通过了就是访问不了数据库请大家帮我改改
客户端程序:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Database_client extends java.applet.Applet implements Runnable,ActionListener
{
Button 查询;TextField 英文单词_文本框,汉语解释_文本框;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
//int x,y;
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
查询=new Button("查询");
英文单词_文本框=new TextField(10);
汉语解释_文本框=new TextField(10);
//setLayout(null);
//int w=getSize().width;int h=getSize().height;
//x=w/3;y=h/3;
add(new Label("输入要查询的英文单词"));add(英文单词_文本框);
add(new Label("汉语解释:"));add(汉语解释_文本框);
add(查询);
查询.addActionListener(this);
}
public void star()
{
try
{
socket=new Socket("localhost",4331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
if(thread==null)
{
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void destroy()
{
try{out.writeUTF("客户离开");}
catch(IOException e1){}
}
public void run()
{
String s=null;
while(true)
{
try{s=in.readUTF();
}
catch(IOException e)
{汉语解释_文本框.setText("与服务器已断开");break;}
汉语解释_文本框.setText(s);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==查询)
{
/* String s=英文单词_文本框.getText();
if(s!=null)
{
try{
out.writeUTF(s);
}
catch(IOException e1){}
}*/
System.out.println("fjdksjf");
}
}
}
服务器程序:
import java.io.*;
import java.net.*;
import java.util.*;
//import java.applet.*;
import java.sql.*;
public class Database_server
{
/** Creates a new instance of Database_server */
// public Database_server() {
//}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{
try{server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("正在监听");}
try{you=server.accept();}
catch(IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{new Server_thread(you).start();}
else{continue;}
}
}
}
class Server_thread extends Thread
{
Socket socket;
Connection Con=null;Statement Stmt=null;
DataOutputStream out=null;DataInputStream in=null;int n=0;
String s=null;
Server_thread(Socket t)
{
socket=t;
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
try{
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
Stmt=Con.createStatement();
}
catch(SQLException ee){}
}
public void run()
{
while(true)
{
try{s=in.readUTF();
}
catch(IOException e){}
try{
if(!(s.equals("客户离开")))
{n=0;
try{
ResultSet rs=Stmt.executeQuery("SELECT * FROM 表1 WHERE 单词="+"'"+s+"'");
}
catch(SQLException e){}
while(rs.next())
{
String 英文单词=rs.getString("单词");
//catch(SQLException e){}
if(s.equals(英文单词))
{
out.writeUTF(rs.getString("解释"));n=1;break;}
}
if(n==0){out.writeUTF("没有此单词");}
}
else if(s.equals("客户离开"))
{Con.close();
System.out.println("客户离开");
Thread.currentThread().yield();break;
}
sleep(1);
}
catch(InterruptedException e){}
catch(IOException e)
{
try
{
Con.close();
}
catch(SQLException ee){}
System.out.println("客户离开");
Thread.currentThread().yield();break;
}
}
}
}
请大家运行后看看,有什么问题,谢谢了!!!!!!!!!!
数据库是用Access建的其中有一个表名字是表1。字段有单词,解释。都为
字符型。数据源名为test,用户名为gxy,密码为ookk
运行以通过了就是访问不了数据库请大家帮我改改
客户端程序:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Database_client extends java.applet.Applet implements Runnable,ActionListener
{
Button 查询;TextField 英文单词_文本框,汉语解释_文本框;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
//int x,y;
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
查询=new Button("查询");
英文单词_文本框=new TextField(10);
汉语解释_文本框=new TextField(10);
//setLayout(null);
//int w=getSize().width;int h=getSize().height;
//x=w/3;y=h/3;
add(new Label("输入要查询的英文单词"));add(英文单词_文本框);
add(new Label("汉语解释:"));add(汉语解释_文本框);
add(查询);
查询.addActionListener(this);
}
public void star()
{
try
{
socket=new Socket("localhost",4331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
if(thread==null)
{
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void destroy()
{
try{out.writeUTF("客户离开");}
catch(IOException e1){}
}
public void run()
{
String s=null;
while(true)
{
try{s=in.readUTF();
}
catch(IOException e)
{汉语解释_文本框.setText("与服务器已断开");break;}
汉语解释_文本框.setText(s);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==查询)
{
/* String s=英文单词_文本框.getText();
if(s!=null)
{
try{
out.writeUTF(s);
}
catch(IOException e1){}
}*/
System.out.println("fjdksjf");
}
}
}
服务器程序:
import java.io.*;
import java.net.*;
import java.util.*;
//import java.applet.*;
import java.sql.*;
public class Database_server
{
/** Creates a new instance of Database_server */
// public Database_server() {
//}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{
try{server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("正在监听");}
try{you=server.accept();}
catch(IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{new Server_thread(you).start();}
else{continue;}
}
}
}
class Server_thread extends Thread
{
Socket socket;
Connection Con=null;Statement Stmt=null;
DataOutputStream out=null;DataInputStream in=null;int n=0;
String s=null;
Server_thread(Socket t)
{
socket=t;
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
try{
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
Stmt=Con.createStatement();
}
catch(SQLException ee){}
}
public void run()
{
while(true)
{
try{s=in.readUTF();
}
catch(IOException e){}
try{
if(!(s.equals("客户离开")))
{n=0;
try{
ResultSet rs=Stmt.executeQuery("SELECT * FROM 表1 WHERE 单词="+"'"+s+"'");
}
catch(SQLException e){}
while(rs.next())
{
String 英文单词=rs.getString("单词");
//catch(SQLException e){}
if(s.equals(英文单词))
{
out.writeUTF(rs.getString("解释"));n=1;break;}
}
if(n==0){out.writeUTF("没有此单词");}
}
else if(s.equals("客户离开"))
{Con.close();
System.out.println("客户离开");
Thread.currentThread().yield();break;
}
sleep(1);
}
catch(InterruptedException e){}
catch(IOException e)
{
try
{
Con.close();
}
catch(SQLException ee){}
System.out.println("客户离开");
Thread.currentThread().yield();break;
}
}
}
}
请大家运行后看看,有什么问题,谢谢了!!!!!!!!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货