所以这是代码:
**package com.install.main;
import java.sql.*;
public class Executor {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// Database credentials
static final String USER = "eli";
static final String PASS = null;
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//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 database... ");
stmt = conn.createStatement();
System.out.println("Connection: " + conn);
String sql = "CREATE DATABASE STUDENTS";
stmt.executeUpdate(sql);
System.out.println("Database created successfully... ");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample**
这是输出:
Connecting to database...
Creating database...
Connection: com.mysql.jdbc.JDBC4Connection@5e265ba4
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'STUDENTS'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469)
at com.install.main.Executor.main(Executor.java:34)
Goodbye!
你们有没有碰巧知道为什么它试图连接到我刚创建的数据库,为什么它实际上没有创建数据库?我没有为mysql设置任何密码。
如果我执行此声明:
GRANT ALL PRIVILEGES ON . TO 'eli'@'localhost' IDENTIFIED BY 'eli' WITH GRANT OPTION;
05:43:15 GRANT ALL PRIVILEGES ON . TO 'eli'@'localhost' IDENTIFIED BY 'eli' WITH GRANT OPTION Error Code: 1045.
如果我在我的控制台中键入mysql命令,它就会想到 - 它将我作为eli@localhost
登录而不提供任何用户名或密码...我在Ubuntu上 - 这是指我在计算机上的用户名,但错误还在那里 - 我什么也做不了。
如果在命令行上执行此mysql> select * from information_schema.user_privileges;
:
这是我能看到的:
+----------------+---------------+----------------+--------------+
| GRANTEE | TABLE_CATALOG | PRIVILEGE_TYPE | IS_GRANTABLE |
+----------------+---------------+----------------+--------------+
| ''@'localhost' | def | USAGE | NO |
+----------------+---------------+----------------+--------------+
1 row in set (0.00 sec)
...但如果我这样做select user();
:
+---------------+
| user() |
+---------------+
| eli@localhost |
+---------------+
1 row in set (0.00 sec)
我尝试使用sudo dpkg-reconfigure mysql-server-5.5
,但它从未要求我输入新密码 - 只需重新启动我的服务器即可。
抱歉,但这里发生了什么?
答案 0 :(得分:0)
您的问题已在此输出中明确定义
Access denied for user ''@'localhost' to database 'STUDENTS'
您已为数据库'eli'
声明了用户static final String USER = "eli";
但是在输出中你可以看到没有为这个数据库设置用户,我可以立即看到的一个原因是你根本没有真正指向你的数据库。
你应该拥有的是
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
答案 1 :(得分:0)
看起来您还没有为主机eli
(localhost
)创建名为'eli'@'localhost'
的用户,因此MySQL以''@'localhost'
为您登录( any_user [at] localhost)并且该用户在MySQL服务器上没有CREATE权限。
因此,在MySQL中为'eli'@'localhost'
( not 'eli'@'%'
)创建一个用户,并授予该用户CREATE权限。