大家看一下下面的代码,总是有空指针的错误!!

CilentSocket.java:

package controller;
import java.io.*;
import java.net.Socket; public class ClientSocket { public static void main String args[]{ try{ // 创建一个流套接字并将其连接到指定 IP 地址的指定端口号 Socket socket =new Socket"127.0.0.1",10000; // 60s超时 socket.setSoTimeout60000; System.out.println"Hello!Server!"; /** 用于获取服务端传输来的信息 */ // 由Socket对象得到输入流,并构造相应的BufferedReader对象 BufferedReader bufferedReader =new BufferedReadernew InputStreamReadersocket.getInputStream; // 输入读入一字符串 String result = bufferedReader.readLine; System.out.println"Server say : " + result; /** 关闭Socket*/ //bufferedReader.close; //socket.close; }catch Exception e { System.out.println"Exception:" + e; } SQLProcessor.initSQL; MainGUI frame = new MainGUI; frame.setVisibletrue; } public static void clientrequestString s{ try { Socket socket = new Socket"127.0.0.1", 10000; // 60s超时 socket.setSoTimeout60000; System.out.println"Hello!Server!"; /** 发送客户端准备传输的信息 */ // 由Socket对象得到输出流,并构造PrintWriter对象 PrintWriter printWriter = new PrintWritersocket.getOutputStream, true; // 将输入读入的字符串输出到Server String sysbuff = s; //BufferedReader sysBuff =new BufferedReadernew InputStreamReaderSystem.in; printWriter.printlnsysbuff; // 刷新输出流,使Server马上收到该字符串 printWriter.flush; }catchIOException e{ e.getMessage; } }
} 
 package socketserver; import controller.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.sql.Timestamp; public class SocketServer { public static void mainString[] args { try { /** 创建ServerSocket*/ // 创建一个ServerSocket在端口2013监听客户请求 //SQLProcessor.initSQL; //System.out.println"服务器连接成功!"; ServerSocket serverSocket =new ServerSocket10000; while true { // 侦听并接受到此Socket的连接,请求到来则产生一个Socket //对象,并继续执行 //System.out.println"服务器连接成功!"; Socket socket = serverSocket.accept; System.out.println"服务器连接成功!"; /** 获取客户端传来的信息 */ // 由Socket得到输入流,并构造相应的BufferedReader对象 BufferedReader bufferedReader =new BufferedReadernew InputStreamReadersocket.getInputStream; // 获取从客户端读入的字符串 String result = bufferedReader.readLine; System.out.println"Client say : " + result; String[] str = result.split","; System.out.printlnstr[0]; switch str[0]{ case "update": DataBase.updateUserstr[1],str[2]; break; case "update1": DataBase.updateUser2str[1],str[2]; break; case "deletedoc": DataBase.deleteDocstr[1]; break; case "deleteuser": DataBase.deleteUserstr[1]; break; case "insertuser": DataBase.insertUserstr[1],str[2],str[3]; break; case "insertdoc": Timestamp timestamp = new TimestampSystem.currentTimeMillis; DataBase.insertDocstr[1],str[2],timestamp,str[3],str[4]; break; case "updatepassword": DataBase.updatePasswordstr[1],str[2]; break; case "uploadfile": DataBase.uploadFilestr[1],str[2],str[3],str[4]; break; case "download": DataBase.downloadFilestr[1]; break; } /** 发送服务端准备传输的 */ // 由Socket对象得到输出流,并构造PrintWriter对象 PrintWriter printWriter =new PrintWritersocket.getOutputStream; printWriter.print"hello Client, I am Server!"; printWriter.flush; /** 关闭Socket*/ //printWriter.close; //bufferedReader.close; //socket.close; } }catch Exception e { System.out.println"Exception:" + e; }finally{ // serverSocket.close; }
}
}

上面分别是客户端和服务器端的代码,下面是数据库的代码:

数据库初始化:

package controller;
import java.sql.*; public class SQLProcessor { private static Connection conn; private static Statement stmt; public static void initSQL { try { String DB_URL = "jdbc:mysql://localhost/guchenghao?" + "user=root&password=gu123&" + "useUnicode=true&characterEncoding=UTF8&useSSL=false"; Class.forName"com.mysql.jdbc.Driver"; System.out.println"数据库初始化成功!"; System.out.println"Connecting to database..."; conn = DriverManager.getConnectionDB_URL; System.out.println"Creating statement..."; stmt = conn.createStatement; } catch Exception e { e.printStackTrace; } } public static ResultSet exeQueryString sql throws SQLException { ResultSet rs = stmt.executeQuerysql; return rs; } public static void exeUpdateString sql throws SQLException { stmt.executeUpdatesql; } public static void disconnectFromDB { try{ conn.close; stmt.close; //statement关闭会导致Resultset关闭,因此不必关闭rs }catch SQLException sqlException { sqlException.printStackTrace; } } }

数据库操作:

package controller; import java.io.*;
import java.util.*;
import java.sql.*; import javax.swing.*; public class DataBase { public static void updateUser2String name,String Password { String sql = "update user set name="+name+" where password="+Password+""; try{ SQLProcessor.exeUpdatesql; }catchSQLException e{ e.getMessage; } } public static void updateUserString role,String name { String sql = "update user set role="+role+" where name="+name+""; try{ SQLProcessor.exeUpdatesql; }catchSQLException e{ e.getMessage; } } public static void updatePasswordString password,String name { String sql = "update user set password="+password+" where name="+name+""; try{ SQLProcessor.exeUpdatesql; }catchSQLException e{ e.getMessage; } } public static User searchUserString name throws SQLException,IllegalStateException { String sql = "select * from user where name=" +name+ ""; ResultSet rs = SQLProcessor.exeQuerysql; ifrs.next { User user = new User rs.getString"name", rs.getString"password", rs.getString"role" ; return user; } return null; } public static Doc searchDocString ID throws SQLException,IllegalStateException { String sql = "select * from files where id="+ID+""; ResultSet rs = SQLProcessor.exeQuerysql; ifrs.next { Doc doc = new Doc rs.getString"id", rs.getString"creator", rs.getTimestamp"timestamp", rs.getString"description", rs.getString"filename" ; return doc; } return null; } public static boolean insertDocString ID, String creater, Timestamp timestamp, String description, String filename throws SQLException,IllegalStateException { String sql = "select * from files where id ="+ID+""; ResultSet rs = SQLProcessor.exeQuerysql; if rs.next return false; else { sql = "insert docs values"" + ID + "","" + filename + "","" + creater + "","" + timestamp + "","" + description + """; SQLProcessor.exeUpdatesql; return true; } } public static Vector<Vector<String >> listAllDocs throws SQLException,IllegalStateException { Vector<Vector<String >> data = new Vector<Vector<String >>; String sql; sql = "select * from files"; ResultSet rs = SQLProcessor.exeQuerysql; whilers.next { Vector<String> row = new Vector<>; row.addrs.getString"ID"; row.addrs.getString"filename"; row.addrs.getString"creator"; row.addrs.getString"timestamp"; row.addrs.getString"description"; data.addrow; } return data; } public static Vector<Vector<String >> listAllUser throws SQLException,IllegalStateException { Vector<Vector<String >> data = new Vector<Vector<String >>; String sql; sql = "select name,role from user"; ResultSet rs = SQLProcessor.exeQuerysql; whilers.next { Vector<String> row = new Vector<>; row.addrs.getString"name"; row.addrs.getString"role"; data.addrow; } return data; } public static boolean insertUserString name, String password, String role throws SQLException,IllegalStateException { String sql = "SELECT * FROM user where name = "+ name +""; ResultSet rs = SQLProcessor.exeQuerysql; if rs.next { return false; }else{ sql = "insert user values" + name + "," + password + "," + role + ""; SQLProcessor.exeUpdatesql; return true; } } public static boolean deleteUserString name throws SQLException,IllegalStateException { String sql = "SELECT * FROM user where name = " + name + ""; ResultSet rs = SQLProcessor.exeQuerysql; if rs.next{ sql = "delete from user where name = " + name + ""; SQLProcessor.exeUpdatesql; return true; }else return false; } public static boolean deleteDocString id throws SQLException,IllegalStateException { String sql = "SELECT * FROM files where id = " + id + ""; ResultSet rs = SQLProcessor.exeQuerysql; if rs.next { sql = "delete from files where id = " + id + ""; SQLProcessor.exeUpdatesql; return true; } else return false; } public static Boolean uploadFileString id, String creater, String description, String filename { try { Timestamp timestamp = new TimestampSystem.currentTimeMillis; // System.out.println"请输入档案文件名:"; // String filename = new String; // filename = scanner.next; // scanner.nextLine; // File f = new File"/Users/Jason/downloadfile/" + filename; // FileInputStream fin = new FileInputStreamf; // InputStreamReader reader = new InputStreamReaderfin, "UTF-8"; // // StringBuffer sb = new StringBuffer; // // while reader.ready { // sb.appendchar reader.read; // // 转成char加到StringBuffer对象中 // } // writer.appendsb.toString; if DataBase.insertDocid, creater, timestamp, description, filename { System.out.println"插入成功"; File f1 = new File"/users/guchenghao/uploadfile/" + filename; BufferedOutputStream fout = new BufferedOutputStream new FileOutputStreamf1; OutputStreamWriter writer = new OutputStreamWriterfout, "UTF-8"; writer.appendid + " " + filename + " " + creater + " " + timestamp + " " + description; writer.close; fout.close; return true; } else { System.out.println"ID已存在,插入失败"; return false; } // reader.close; // fin.close; }catch Exception e { e.printStackTrace; } return false; } public static Boolean downloadFileString id { try { Doc doc = DataBase.searchDocid; if doc == null { System.out.println"null"; return false; } else { File f = new File"/users/guchenghao/downloadfile/" + doc.getFilename.toString; BufferedOutputStream fout = new BufferedOutputStream new FileOutputStreamf; OutputStreamWriter writer = new OutputStreamWriterfout, "UTF-8"; writer.appenddoc.getID + " " + doc.getFilename + " " + doc.getCreator + " " + doc.getTimestamp + " " + doc.getDescription; writer.close; fout.close; System.out.print"下载成功"; return true; } } catch Exception e { e.printStackTrace; } return false; } }

错误:

你的数据库操作是在server端的吧,可是你是在client端启动的的数据库,那么server端deleteUser的时候数据库是没连接的,就报空指针异常了

发表评论

电子邮件地址不会被公开。 必填项已用*标注