2013-02-02 38 views
-1

对不起,这个愚蠢的问题。问题解决了。正确的问题是我想在套接字中发送数据库连接。哪些实际意识到不可能发生,因为套接字之间的信息作为消息传递。再次,我为这个愚蠢的问题表示歉意。如果您撤回反对票,将非常感激。如何将数据从服务器发送到JAVA中的客户端套接字

package Controller; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 

import View.LoginInChat; 


public class ChatServer { 

    private static final int PORT = 51000; 
    private static String dbURL = "jdbc:derby:DesktopChatDatabase"; 
    public static String tableName = "APP.USERS"; 
    // jdbc Connection 
    public static Connection conn = null; 
    private static Statement stmt = null; 
    private ServerSocket serverSocket = null; 


    public static void main(String[] args) { 
     ChatServer s = new ChatServer(); 
     try { 
      s.run(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
    public void run() throws IOException{ 
     serverSocket = new ServerSocket(PORT); 
     createConnection(); 
     boolean running = true; 
     while(running){ 
      LoginInChat.setConnectionDatabase(getConn()); 
      System.out.println(getConn()); 


      Socket client = serverSocket.accept(); 
      System.out.println(client); 

     } 

     serverSocket.close(); 
    } 

    public static boolean createConnection() 
    { 
     boolean exceptionFlag = true; 
     try 
     { 
      Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); 
      //Get a connection 
      setConn(DriverManager.getConnection(dbURL)); 
     } 
     catch (Exception except) 
     { 
      except.printStackTrace(); 
     } 

     return exceptionFlag; 
    } 


    public static void shutdown() 
    { 
     try 
     { 
      if (getStmt() != null) 
      { 
       getStmt().close(); 
      } 

      if (getConn() != null) 
      { 
       DriverManager.getConnection(dbURL + ";shutdown=true"); 
       getConn().close(); 
      }   
     } 
     catch (SQLException sqlExcept) 
     { 

     } 

    } 

    public static Statement getStmt() { 
     return stmt; 
    } 

    public static void setStmt(Statement stmt) { 
     ChatServer.stmt = stmt; 
    } 

    public static Connection getConn() { 
     return conn; 
    } 

    public static void setConn(Connection conn) { 
     ChatServer.conn = conn; 
    } 


} 

这里是谁开始

package View; 

import java.io.File; 
import java.io.IOException; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.sql.Connection; 

import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.layout.FormLayout; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.FormData; 
import org.eclipse.swt.layout.FormAttachment; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.widgets.Button; 
import Controller.LoginInInterChat; 

public class LoginInChat { 

    protected static Shell shell; 
    private Text UsernameText; 
    private Text PasswordText; 
    private final static String ImageLabel = "InterChatIcons" + File.separator + "ChatIconLogin.png"; 
    protected final static String ImageProgram = "InterChatIcons" + File.separator + "chatIcon.ico"; 
    protected Display display; 

    private static final int PORT = 51000; 
    private static final String HOST = "localhost"; 
    private static Socket clientSocket = null; 
    public static Connection connectionDatabase; 

    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try { 
      LoginInChat window = new LoginInChat(); 
      window.open(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Open the window. 
    */ 
    public void open() { 
     display = Display.getDefault(); 
     connectUser(); 
     createContents(); 

     System.out.println("Client Socket = " + getClientSocket()); 
     shell.open(); 
     shell.layout(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 

     System.out.println(getConnectionDatabase()); 
    } 

    /** 
    * Create contents of the window. 
    */ 
    protected void createContents() { 
     shell = new Shell(SWT.MIN); 
     shell.setSize(450, 300); 
     shell.setText("InterChat"); 
     shell.setLayout(new FormLayout()); 
     shell.setImage(new Image(display, ImageProgram)); 

     UsernameText = new Text(shell, SWT.BORDER); 
     FormData fd_UsernameText = new FormData(); 
     fd_UsernameText.right = new FormAttachment(0, 223); 
     fd_UsernameText.top = new FormAttachment(0, 51); 
     fd_UsernameText.left = new FormAttachment(0, 10); 
     UsernameText.setLayoutData(fd_UsernameText); 

     Label UsernameLabel = new Label(shell, SWT.NONE); 
     FormData fd_UsernameLabel = new FormData(); 
     fd_UsernameLabel.bottom = new FormAttachment(UsernameText, -8); 
     fd_UsernameLabel.left = new FormAttachment(0, 10); 
     UsernameLabel.setLayoutData(fd_UsernameLabel); 
     UsernameLabel.setText("Username"); 

     PasswordText = new Text(shell, SWT.BORDER); 
     FormData fd_PasswordText = new FormData(); 
     fd_PasswordText.right = new FormAttachment(0, 223); 
     fd_PasswordText.top = new FormAttachment(UsernameText, 38); 
     fd_PasswordText.left = new FormAttachment(0, 10); 
     PasswordText.setLayoutData(fd_PasswordText); 

     Label PasswordLabel = new Label(shell, SWT.NONE); 
     FormData fd_PasswordLabel = new FormData(); 
     fd_PasswordLabel.bottom = new FormAttachment(PasswordText, -6); 
     fd_PasswordLabel.left = new FormAttachment(UsernameText, 0, SWT.LEFT); 
     PasswordLabel.setLayoutData(fd_PasswordLabel); 
     PasswordLabel.setText("Password"); 

     Button LoginButton = new Button(shell, SWT.NONE); 
     FormData fd_LoginButton = new FormData(); 
     fd_LoginButton.top = new FormAttachment(PasswordText, 41); 
     fd_LoginButton.right = new FormAttachment(UsernameText, 0, SWT.RIGHT); 
     LoginButton.setLayoutData(fd_LoginButton); 
     LoginButton.setText("Login"); 
     LoginButton.addSelectionListener(new LoginInInterChat()); 

     Label LogoInterChat = new Label(shell, SWT.NONE); 
     FormData fd_LogoInterChat = new FormData(); 
     fd_LogoInterChat.bottom = new FormAttachment(LoginButton, 0, SWT.BOTTOM); 
     fd_LogoInterChat.top = new FormAttachment(UsernameText, -41, SWT.TOP); 
     fd_LogoInterChat.left = new FormAttachment(UsernameText, 35); 
     fd_LogoInterChat.right = new FormAttachment(100, -2); 
     LogoInterChat.setLayoutData(fd_LogoInterChat); 
     LogoInterChat.setImage(new Image(display, ImageLabel)); 

     System.out.println("Conne is " + getConnectionDatabase()); 

    } 

    public static Connection getConnectionDatabase() { 
     return connectionDatabase; 
    } 

    public static void setConnectionDatabase(Connection connectionDatabase) { 
     LoginInChat.connectionDatabase = connectionDatabase; 
    } 

    public static void connectUser() 
    { 
     try { 
      setClientSocket(new Socket(HOST, PORT)); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void setClientSocket(Socket clientSocket) { 
     LoginInChat.clientSocket = clientSocket; 
    } 

    public static Socket getClientSocket() { 
     return clientSocket; 
    } 
} 
+0

什么是“库托”?什么是问题? (不是我的投票) – Dave

+0

库托相当于类(编辑我的文章) – Krasimir

+0

标题中的问题的答案是'客户端发送数据到服务器的相同方式'。如果你有一个空值变量,那是因为你还没有分配它或者你已经将它设置为空。不是一个真正的问题。 – EJP

回答

0

你试图使用德比客户 JDBC驱动程序的客户端,但你的连接网址是在嵌入式 JDBC语法德比

要么使用Derby嵌入式JDBC驱动程序,要么将连接URL语法更新为适当的客户端语法格式(jdbc:derby:// host:port/databaseName)。

These docs将帮助您了解区别。

+0

问题修改 – Krasimir

相关问题