2012-04-15 9 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; 

     } 
    } 
} 

Ich verwende dies auf Amazon EC2 mit Port 4001 offen.Wie kann man MySQL, das auf Amazon EC2 läuft, mit Android/Java Application verbinden?

ich erfolgreich bin in Socket-Kommunikation, aber wenn ich eine Verbindung mysql lokal ausgeführt wird, erhalte ich Null-Zeiger-Ausnahme bei

dpProvider.connect(); 

ich dies mit jdbc MySQL Connector namens mysql_conn.jar nenne als:

javac -classpath ./mysql_conn.jar ClientHandler.java 

was gut kompiliert.

aber wenn ich rufe: java ClientHandler

Null-Zeiger-Ausnahme auftritt ..

Bitte um Hilfe ..!

+0

BTW, diese Klasse läuft auf dem EC2 Linux Server. –

Antwort

0

Versuchen Sie, mysql_conn.jar dem Klassenpfad hinzuzufügen, wenn Sie den Java-Befehl z. java -cp ClientHandler

Verwandte Themen