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 に接続すると、null ポインター例外が発生します。

dpProvider.connect();

mysql_conn.jar という名前の jdbc mysql コネクタを使用してこれを呼び出しています。

javac -classpath ./mysql_conn.jar ClientHandler.java

これは正常にコンパイルされます。

しかし、私が電話すると:java ClientHandler

ヌルポインタ例外が発生。

助けてください..!

4

1 に答える 1

0

java コマンド (例: java -cp ClientHandler) を発行するときに、クラスパスに mysql_conn.jar を追加してみてください。

于 2012-04-15T11:53:06.817 に答える