我收到此错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect database name ' '
在下面运行我的代码之后。
请帮忙。我的数据库在xampp版本3.22中运行。我正在使用j连接器5.1
package connect;
import java.sql.*;
public class MY_Database {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:mysql://localhost:3306/ ";
String user = "root";
String password = "";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con= DriverManager.getConnection(url,user,password);
Statement stt = con.createStatement();
//Create and Save db
stt.execute("Create Database");
stt.execute("Use the test ");
//Create out table
stt.execute("DROP TABLE IF EXIST people");
stt.execute("CREATE TABLE people("
+"id BIGINT NOT NULL AUTO_INCREMENT,"
+"fname VARCHAR(25), "
+"lname VARCHAR (25),"
+"PRIMARY KEY (id)"
+
" )");
// ADD SOME ENTRIES
stt.execute("INSERT INTO people(fname , lname ) VALUES "+
"('Nathan','Tenderere') ('Praise ''Tenderere') ('Tadiwa
''Machona')");
// Get people with surname Tenderere
ResultSet res = stt.executeQuery("SELECT * FROM people WHERE lname =
'Tenderere' ");
while (res.next()) {
System.out.println(res.getString("fname")+""+
res.getString("lname") );
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
您的数据库名称是什么?您的连接字符串为:
String url = "jdbc:mysql://localhost:3306/ ";
在结尾引用之前有一个尾随空格“。
所以你要说的是你正在尝试连接一个名为“”的数据库,这显然是无效的,删除尾部空格并在连接字符串中包含你的数据库名称。