java.sql.SQLException:未选择数据库-为什么?
java.sql.SQLException:未选择数据库-为什么?,java,mysql,jdbc,odbc,sqlexception,Java,Mysql,Jdbc,Odbc,Sqlexception,最后几天,我试图学习如何通过Java访问mySQL数据库。 我能够加载驱动程序并获得到数据库的连接(至少我这样认为,因为我没有在那里得到异常…) 代码是: import java.sql.*; public class test { public static void main(String[] args) { try { Class.forName("sun.j
最后几天,我试图学习如何通过Java访问mySQL数据库。 我能够加载驱动程序并获得到数据库的连接(至少我这样认为,因为我没有在那里得到异常…) 代码是:
import java.sql.*;
public class test
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("driver loaded...");
}
catch(ClassNotFoundException e){
System.out.println("Error in loading the driver..."+e);
System.exit(0);
}
try
{
Connection dbConnection= DriverManager.getConnection("jdbc:odbc:test","root","password");
System.out.println("Connection successful...");
Statement stmt = dbConntection.createStatement();
stmt.executeUpdate("create table Accounts ( name char(20) )");
}
catch(SQLException e)
{
System.out.println("database-ConnectionError: "+e);
System.exit(0);
}
}
}
当我执行它时,它说:
驱动程序加载…连接成功…
数据库连接错误:java.sql.SQLException:[MySQL][ODBC 5.2(w)驱动程序][mysqld-5.5.31]未选择任何数据库
我真的不知道问题出在哪里,因为我以为数据库是在“getConnection”过程中选择的……
我试图通过添加以下行来选择数据库:
stmt.executeUpdate("use test;");
在创建语句之后。不幸的是,它没有工作,因为我得到了另一个异常,它说我应该检查语法。我也不明白,因为在我的命令行中,它工作得很好。。。 我不知道是否可以通过Java使用这些类型的命令,所以如果不能,请原谅我的错误 我希望你能帮助我,在我自己的搜索过程中,我没有错过解决方案 已经感谢所有回复我的问题并在我的问题上花费时间的人 注:如果我忘记指出一些重要信息(我想我没有),请询问:) 编辑:我还尝试在运行时创建一个新数据库
stmt.executeUpdate("CREATE DATABASE test;");
这实际上是可行的,但是数据库也不会被选择…在添加表之前,首先必须选择一个数据库。 您可以使用以下工具创建新数据库:
CREATE DATABASE database_name
您可以通过以下方式连接到特定数据库:
String url = "jdbc:mysql://localhost/databasename";
String username = "test";
String password = "test";
Connection connection = DriverManager.getConnection(url, username, password);
这是您更新的示例,对我很有用
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("driver loaded...");
} catch (ClassNotFoundException e) {
System.out.println("Error in loading the driver..." + e);
System.exit(0);
}
try {
Connection dbConnection = DriverManager
.getConnection("jdbc:mysql://localhost/test?user=root&password=password");
System.out.println("Connection successful...");
Statement stmt = dbConnection.createStatement();
stmt.executeUpdate("create table Accounts ( name char(20) )");
} catch (SQLException e) {
System.out.println("database-ConnectionError: " + e);
System.exit(0);
}
}
确保已将正确的mysql连接器添加到构建路径中。我使用了:
mysql-connector-java-5.1.24-bin.jar
首先,我在考虑我的答案,以向您展示另一种更好的连接mysql数据库的方法,它更容易,也更少出现异常。您需要执行以下步骤:
.jar
添加到库中,或者有许多tut)import java.sql.*;
public class MySqlConnection {
private String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private String MYSQL_URL = "jdbc:mysql://localhost:3306/test";
private Connection con;
private Statement st;
private ResultSet rs;
public MySqlConnection() {
try {
Class.forName(MYSQL_DRIVER);
System.out.println("Class Loaded....");
con = DriverManager.getConnection(MYSQL_URL,"","");
System.out.println("Connected to the database....");
st = con.createStatement();
int c =st.executeUpdate("CREATE TABLE Accounts (Name VARCHAR(30))");
System.out.println("Table have been created.");
System.out.println(c+" Row(s) have been affected");
con.close();
} catch(ClassNotFoundException ex) {
System.out.println("ClassNotFoundException:\n"+ex.toString());
ex.printStackTrace();
} catch(SQLException ex) {
System.out.println("SQLException:\n"+ex.toString());
ex.printStackTrace();
}
}
public static void main(String...args) {
new MySqlConnection();
}
}
我也有同样的问题。对不起,我忘了说。。。已创建数据库,但仍未选择。。我是如何做到的?我连接到我的数据库的方法是,如果您运行的是localhost,则将连接到数据库的url附加到数据库名称,localhost/DatabaseName更新了我连接到数据库的方式。这个方法在我的程序中运行得很好,我相信这是可能的。。但是为什么要使用odbc而不是mysql呢?非常感谢。我明天会试试你用的连接器!我真的希望我能让它运行^^@seppupuber3:your'r welcome,也许还有ODBC(不确定):)。顺便说一句:对于JDBC驱动程序,您不应该使用像
use
这样的命令在数据库之间切换。相反,您应该使用setCatalog
和/或setSchema
(或在连接字符串中指定数据库)。使用use
可能会导致驾驶员的错误行为。