使用Java中的MysqlDataSource连接到远程数据库

时间:2018-08-16 08:26:42

标签: java mysql database connection datasource

我正在尝试连接到我的远程digitalocean服务器。带有MysqlDataSource对象。我使用的Mysql连接器是Mysql-connector-java-8.0.12.jar。

当我尝试测试连接时,出现以下错误消息。

mongodb@3.0.11

我的服务器已经安装了mysql,我可以通过我的机器上的mysqlworkbench访问它。程序在此行崩溃

 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. com.mysql.cj.jdbc.exceptions.CommunicationsException The last 
 packet sent successfully to the server was 0 milliseconds ago. The driver 
 has not received any packets from the server.

这是我的连接器类:

Connection connection = dataSource.getConnection();

要求的编辑: 这是我用来执行查询的课程

package DataLayer;


 import com.mysql.cj.jdbc.MysqlDataSource;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;

public class Connector {

private final MysqlDataSource dataSource = new MysqlDataSource();
private Connection con;

Connection getConnection() throws SQLException {
    dataSource.setServerName("myRemoteIP");
    dataSource.setPort(3306);
    dataSource.setDatabaseName("jsquiz");
    dataSource.setUser("DBusername");
    dataSource.setPassword("DBpassword");
    Connection connection = dataSource.getConnection();
    return connection;
}

public void openConnection() throws SQLException {
    if (con == null) {
        con = getConnection();
    }
}

public void closeConnection() {
    if (con != null) {
        con = null;
    }
}

public ResultSet getResultSet(String sql) throws SQLException {
    openConnection();
    Statement st = con.createStatement();
    closeConnection();
    return st.executeQuery(sql);
}

public void executeSQL(String sql) throws SQLException {
    openConnection();
    Statement st = con.createStatement();
    st.executeUpdate(sql);
    closeConnection();
}
}

这就是我叫代码的地方

 package DataLayer;

 import java.sql.ResultSet;
 import java.sql.SQLException;

 public class DBOperator {

     Connector con = new Connector();

     public void createQuestion(String question) throws SQLException {
         String sql = "INSERT INTO `jsquiz`.`questions` (`question`) VALUES 
 ('" + question + "');";
         con.openConnection();
    con.executeSQL(sql);
    con.closeConnection();
}

public String getQuestion(String question) throws SQLException {
    String sql = "select from `jsquiz`.`questions` where question = " + question + ";";
    con.openConnection();
    ResultSet rs = con.getResultSet(sql);
    while (rs.next()) {
        return rs.getString("question");
    }
    return null;
}

}

完整堆栈

 package DataLayer;

 import org.junit.Test;
 import static org.junit.Assert.*;

 public class DBOperatorTest {

     public DBOperatorTest() {
     }

     @org.junit.Test
     public void testCreateQuestion() throws Exception {
         DBOperator dbop = new DBOperator();
         String question = "not";
         dbop.createQuestion(question);
         assertEquals(dbop.getQuestion(question), question);
     }

 }

0 个答案:

没有答案