简体   繁体   English

用Java连接Mysql数据库

[英]Connecting Mysql database with Java

--CODE UPDATED-- -代码已更新-

I'm trying to connect a MySQL database using JDBC but I'm not able to get the connection to work. 我正在尝试使用JDBC连接MySQL数据库,但无法使连接正常工作。 I'm not sure if the way I've constructed the connection string is right. 我不确定我构造连接字符串的方式是否正确。 Here's the code and the error: 这是代码和错误:

Code: 码:

    Connection conn;
    String userName = "root";
    String serverName = "127.0.0.1";
    String portNumber = "3306";
    String password = "password";
    String dbName = "myDB";

    /**
     * Establishes connection with the sql database
     * @throws SQLException
     */
    public SQLConnector() throws SQLException {
        setConnection();
    }

    private void setConnection() throws SQLException {
        Properties connectionProps = new Properties();
        connectionProps.put("user", this.userName);
        connectionProps.put("password", this.password);
        String connectionString = "jdbc:mysql://" + this.serverName
                + ":" + this.portNumber + "/" + this.dbName;
        System.out.println(connectionString);
        this.conn = DriverManager.getConnection(connectionString, connectionProps);
    }

Error: 错误:

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:627)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1013)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.source.database.SQLConnector.setConnection(SQLConnector.java:69)
    at com.source.database.SQLConnector.<init>(SQLConnector.java:25)
    at com.source.main.Start.main(Start.java:13)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2914)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:559)
    ... 18 more

I did tried connecting mysql with java sometime back. 我确实尝试过将MySQL与Java连接起来。 Here is how i did it. 这是我的方法。

private Connection getConnection() throws SQLException 
    {
        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", this.userName);
        connectionProps.put("password", this.password);

        conn = DriverManager.getConnection("jdbc:mysql://"
                + this.serverName + ":" + this.portNumber + "/" + this.dbName,
                connectionProps);

        return conn;
    }

public boolean executeUpdate(Connection connection, String command) throws SQLException
    {
        try
        {
          Statement st = connection.createStatement();

          st.executeUpdate(command);

          connection.close();
        }
        catch (Exception e)
        {
          System.err.println("Got an exception!");
          e.printStackTrace();
        }

        return false;
    }

executeUpdate was used to update, insert and delete from the tables. executeUpdate用于更新,插入和从表中删除。 Hope that helped.. 希望能有所帮助。

Oh, and also JDBC driver must be in the program path. 哦,而且JDBC驱动程序也必须位于程序路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM