Netbeans 与 mysql 的交流
Netbeans and mysql communication
我已经为我们公司创建了一个应用程序。起初,我尝试在 MySQL-xampp 中的数据库中创建无密码的方法,但效果很好。现在我的问题是我在我的数据库上设置了密码,现在我的应用程序在我登录时抛出这个错误:
SQL java.sql.SQLException: Access denied for user
'root'@'CITSP-MOB7'(using password: YES)
我在连接数据库时也遇到错误:
Unable to connect. Cannot establish a connection to
jdbc:mysql//localhost:3306/mydb using com.mysql.jdbc.Driver (Access
denied for user 'root'@'CITSP-MOB7' (using password: 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();
有人可以帮我解决连接问题吗?
您的回答将不胜感激。提前致谢! :)
如果适合您,请在 mysql 中测试创建新用户。
首先在mysql中创建一个新用户:
以 root 身份从 mysql 命令行登录。然后运行下面一条一条的命令。
MySQL
创建用户 'mydbuser'@'localhost' 由 'asdf';
识别
将 . 上的所有权限授予 'mydbuser'@'localhost' ;
刷新权限;
将上面的 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);
}
我已经为我们公司创建了一个应用程序。起初,我尝试在 MySQL-xampp 中的数据库中创建无密码的方法,但效果很好。现在我的问题是我在我的数据库上设置了密码,现在我的应用程序在我登录时抛出这个错误:
SQL java.sql.SQLException: Access denied for user 'root'@'CITSP-MOB7'(using password: YES)
我在连接数据库时也遇到错误:
Unable to connect. Cannot establish a connection to jdbc:mysql//localhost:3306/mydb using com.mysql.jdbc.Driver (Access denied for user 'root'@'CITSP-MOB7' (using password: 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();
有人可以帮我解决连接问题吗? 您的回答将不胜感激。提前致谢! :)
如果适合您,请在 mysql 中测试创建新用户。
首先在mysql中创建一个新用户: 以 root 身份从 mysql 命令行登录。然后运行下面一条一条的命令。
MySQL
创建用户 'mydbuser'@'localhost' 由 'asdf';
识别
将 . 上的所有权限授予 'mydbuser'@'localhost' ;
刷新权限;
将上面的 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);
}