这个代码:
package database;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DataBase {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/javadb", "root", "123");
System.out.println("sadsa");
ConnectToDatabase db = new ConnectToDatabase("javadb", "root", "123");
System.out.println("sadsa");
String sqlStatement = "SELECT *\n" +
"FROM `javadb`.`user`\n" +
" LIMIT 1000;";
ResultSet result = db.makeQuery(sqlStatement);
while (result.next()) {
System.out.println(result.getString("ID")
+ " " + result.getString("UserName")
+ " " + result.getString("Password")
);
}
db.closeConnection();
}
}
分离的班级:
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectToDatabase {
// Database Variables
private final String databaseName;// = "jdbc:mysql://localhost/javadb"; //URL
private final String databaseUserName;// = "root";
private final String databasePassword;// = "123";
private Connection connect;
private Statement sqlStatement;
// Connect to Database
public ConnectToDatabase( String givenDatabaseName,String givenDatabaseUserName, String givenDatabasePassword) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
this.databaseName = givenDatabaseName;
this.databaseUserName = givenDatabaseUserName;
this.databasePassword = givenDatabasePassword;
this.connect = DriverManager.getConnection("jdbc:mysql://localhost/"+givenDatabaseName, givenDatabaseName, givenDatabasePassword);
this.sqlStatement = this.connect.createStatement();
}
public ResultSet makeQuery(String givenStatement) throws SQLException{
setSqlStatement(givenStatement);
return sqlStatement.executeQuery(givenStatement);
}
public String getDatabaseUserName() {
return databaseUserName;
}
public String getDatabasePassword() {
return databasePassword;
}
public String getDatabaseName() {
return databaseName;
}
public void setSqlStatement(Statement sqlStatement) {
this.sqlStatement = sqlStatement;
}
public void closeConnection() throws SQLException{
connect.close();
}
private void setSqlStatement(String givenStatement) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
注意: 在主要方法中,如果我写相同的信息直接连接,这将正常工作:
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/javadb", "root", "123");
这是错误消息:
Exception in thread "main" java.sql.SQLException: Access denied for user 'javadb'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4615)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1302)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.ConnectToDatabase.<init>(ConnectToDatabase.java:27)
at database.DataBase.main(DataBase.java:15)
Java Result: 1
非常感谢
答案 0 :(得分:0)
创建ConnectToDatabase
实例时,可能没有传递正确的参数值。你能展示一部分代码吗?
答案 1 :(得分:0)
您需要指定端口号。 像
jdbc:mysql://localhost:3306/javadb
答案 2 :(得分:0)
您必须提前指定端口号,但缺少分隔的类端口号。
答案 3 :(得分:0)
Pablo Santa Cruz: 它:
呼叫:
ConnectToDatabase db = new ConnectToDatabase("javadb", "root", "123");
方法:
public ConnectToDatabase( String givenDatabaseName,String givenDatabaseUserName, String givenDatabasePassword) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
this.databaseName = givenDatabaseName;
this.databaseUserName = givenDatabaseUserName;
this.databasePassword = givenDatabasePassword;
this.connect = DriverManager.getConnection("jdbc:mysql://localhost/"+givenDatabaseName, givenDatabaseName, givenDatabasePassword);
// "jdbc:mysql://localhost/?user=root&password=rootpassword"
// connect = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
this.sqlStatement = this.connect.createStatement();
}
答案 4 :(得分:0)
在ConnectTo数据库类
中为DriverManager类提供用户名和密码时出错this.connect = DriverManager.getConnection(&#34; jdbc:mysql:// localhost /&#34; + givenDatabaseName,givenDatabaseName,givenDatabasePassword);
这里给出两次Database(givenDatabaseName)名称而不是Database user(givenDatabaseUserName)名称;
希望有所帮助