socket编程并连接数据库问题!! 请来看一下,帮帮忙!!
下面的代码是一个ATM服务器端的代码,想要与数据库连接并能与客户端通信,但是下面程序运行的时候老是在out.writeUTF(Smessage); new Communicate("用户名或密码错误!\n请重新输入!");
new Connect(inMsage); 处出现
空指针错误,找了半天找不出原因,估计其他地方也有错误,刚开始学java对这些错误有些不知所措,请各位帮忙看一下是什么问题,谢谢!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.sql.*;
public class csServerATM {
@SuppressWarnings("resource")
public static void main(String[] args){
ServerSocket server=null;
//Communicate thread;
Socket visitor=null;
while(true){
try{
server=new ServerSocket(4422);
//System.out.println("linked successfully");
}
catch(
IOException e){System.out.println(""+e);break;}
try{ visitor=server.accept(); }
catch(IOException e1){System.out.println(""+e1);break;}
if(visitor!=null){
new Communicate(visitor).start();
}
else continue;
}
}
}
class Communicate extends Thread{
Socket cSocket;
ServerSocket server;
DataInputStream in;
DataOutputStream out;
String Smessage="";
Communicate(String mes){
while(true){
Smessage=mes;
try{
out.writeUTF(Smessage);
}
catch(IOException e1){System.out.println(""+e1);break;}
}}
Communicate(Socket t) {
try{
cSocket=t;
in=new DataInputStream(cSocket.getInputStream());
out=new DataOutputStream(cSocket.getOutputStream());
//out.writeUTF("successfully");
System.out.println("连接成功!");
}catch(IOException e){System.out.println("无法连接"+e);}
}
public void run(){
String inMsage="";
while(true){
try{
inMsage=in.readUTF();
//System.out.println(inMsage);
new Connect(inMsage);
}
catch(IOException e){System.out.println(""+e);break;}
}
}
public void SendMessage(String mes){
}
}
class Connect {
Connection dbCon;
ResultSet rs;
Statement sql;
Connect(String message){
String msage=message;
//System.out.println("shujuku"+msage);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(
ClassNotFoundException e){System.out.println("驱动失败!");}
try{
dbCon=DriverManager.getConnection("jdbc:odbc:bankAccount","","");
System.out.println("数据库连接成功");
if(msage.substring(0, 5).equals("query")){
if(msage.length()==14){
String accountNu=msage.substring(5,14);
String psw=msage.substring(14,msage.length());
sql=dbCon.createStatement();