0
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;

public class ClientHandler {
    static ServerSocket providerSocket;
    static Socket connection = null;
    static ObjectOutputStream out;
    static ObjectInputStream in;
    static String message;
    static DBProvider dbProvider;

    public static void main(String args[]) {

        // while (true) {
        ClientHandler.run();
        // }
    }

    static void run() {
        try {
            // 1. creating a server socket
            providerSocket = new ServerSocket(4001, 10);
            // 2. Wait for connection
            System.out.println("Waiting for connection at "
                    + providerSocket.getLocalPort());
            connection = providerSocket.accept();
            System.out.println("Connection received from "
                    + connection.getInetAddress().getHostAddress());

            // 3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");

            // 4. The two parts communicate via the input and output streams
            message = (String) in.readObject();

            dbProvider.connect();
            // gets the messages from client here
            dbProvider.insert_delete_entries(message);

        } catch (ClassNotFoundException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } catch (IOException ioException) {
            System.out.println(ioException.toString());
            ioException.printStackTrace();
        } catch (SQLException e) {
            System.out.println(e.toString());
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
            e.printStackTrace();
        } finally {
            // 4: Closing connection
            try {
                dbProvider.disconnect();
                in.close();
                out.close();
                providerSocket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            } catch (NullPointerException e) {
                System.out.println(e.toString());
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    static void sendMessage(String... msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("server>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    static void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();

        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public class DBProvider {
        final static String userName = "root";
        final static String password = "password";
        final static String url = "jdbc:mysql://localhost:3306/central_server";
        protected Connection conn = null;

        DBProvider() {

        }

        public void connect() throws ClassNotFoundException, SQLException,
                IllegalAccessException {
            // TODO Auto-generated method stub
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("Database connection established");
            System.out.println(conn.toString());

        }

        public void disconnect() throws SQLException {
            // TODO Auto-generated method stub
            if (conn != null) {
                conn.close();
                System.out.println("Database connection terminated");

            }
        }

        public void insert_delete_entries(String string) throws SQLException {
            // TODO Auto-generated method stub

            Statement delete = conn.createStatement();
            int i = delete.executeUpdate(string);
            System.out.println(i + " row(s) affected");

        }

        public ResultSet display_results(String string) throws SQLException {
            // TODO Auto-generated method stub

            Statement displayStatement = conn.createStatement();
            ResultSet result = displayStatement.executeQuery(string);
            System.out.println(result.toString());
            return result;

        }
    }
}

我在打开端口 4001 的 Amazon EC2 上运行它。

我在套接字通信中成功,但是当我连接到本地运行的 mysql 时,出现空指针异常

dpProvider.connect();

我使用名为 mysql_conn.jar 的 jdbc mysql 连接器调用它:

javac -classpath ./mysql_conn.jar ClientHandler.java

编译得很好。

但是当我打电话时:java ClientHandler

出现空指针异常。。

请帮忙..!

4

1 回答 1

0

尝试在发出 java 命令时将 mysql_conn.jar 添加到类路径,例如 java -cp ClientHandler

于 2012-04-15T11:53:06.817 回答