-1

对不起这个愚蠢的问题。问题已解决。正确的问题是我想在套接字中发送数据库连接。这实际上意识到不可能发生,因为套接字之间的信息是作为消息传递的。再次,我为这个愚蠢的问题道歉。如果您撤回反对票,将不胜感激。

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;
    }
}
4

1 回答 1

0

您正在尝试使用 Derby客户端JDBC 驱动程序,但您的连接 URL 采用 derby嵌入式JDBC 语法。

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

这些文档将帮助您了解其中的区别。

于 2013-02-03T17:39:34.270 回答