2012-04-15 78 views
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上运行此操作。如何使用Android/Java应用程序连接在Amazon EC2上运行的MySQL?

我成功套接字通信,但是当我连接到MySQL本地运行,我在

dpProvider.connect(); 

获得空指针异常,我呼吁这个使用名为mysql_conn.jar mysql的jdbc连接器:

javac -classpath ./mysql_conn.jar ClientHandler.java 

编译得很好。

但是当我打电话:发生java ClientHandler

空指针异常..

请帮助..!

+0

顺便说一下,这个类在EC2 Linux服务器上运行。 – 2012-04-15 11:47:54

回答

0

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

相关问题