如何使用Android / Java应用程序连接在Amazon EC2上运行的MySQL?

时间:2012-04-15 11:46:12

标签: mysql jdbc

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

发生空指针异常..

请帮助..!

1 个答案:

答案 0 :(得分:0)

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