我为我们公司创建了一个应用程序。一开始,我尝试在MySQL-xampp数据库上创建没有密码的内容,它运行得很好。现在我的问题是,我在数据库中输入了一个密码,每当我登录时,我的应用程序就会抛出这个错误:
SQL java.sql.SQLException:拒绝用户‘root’@‘CITSP 7’的访问(使用密码: YES)
连接数据库时也会出现一个错误:
无法连接。无法使用com.mysql.jdbc.Driver建立到jdbc:mysql//localhost:3306/mydb的连接(用户'root'@'CITSP-MOB7‘被拒绝访问(使用密码: YES))。
下面是我的数据库连接代码:
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://192.168.1.112:3306/citsp";
static final String USER = "root";
static final String PASS = "mypassword";
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
}
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
有人能帮我解决我的连接问题吗?您的回答将不胜感激。谢谢!)
发布于 2015-04-22 06:25:45
如果这对您有用,请测试在mysql中创建一个新用户。
首先,在mysql中创建一个新用户:以root用户身份从mysql命令行登录。然后逐一运行以下命令。
MySQL
将上述java代码更改为:我假定数据库名为'citsp'
JAVA
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost/citsp";
String USER = "mydbuser";
String PASS = "asdf";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
//JOptionPane.showMessageDialog(null, "There's an error connecting with the database. Please contact your administrator.");
}
//STEP 3: Open a connection
System.out.println("Connecting to database...");
java.sql.Connection conn = null;
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
//STEP 4: Execute a query
System.out.println("Creating statement...");
try {
Statement stmt = conn.createStatement();
String query = "";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
// Iterate through the result
}
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
https://stackoverflow.com/questions/29787434
复制相似问题