无法运行查询java mysql数据库

时间:2016-09-03 13:40:04

标签: java mysql

我无法在mysql数据库中运行查询。抛出异常: 数据库名称是否正确仍然无法理解该问题是什么 在Java Eclipse中运行。使用Mysql Jar:external/mysql-connector-java-5.1.16-bin.jar

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'autodist' at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885) at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3436) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775) at com.mysql.jdbc.Connection.<init>(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) 我的代码:

try {
  DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  Class.forName("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection("jdbc:mysql://localhost/autodist","root","1234");

  System.out.println(conn.toString());
} catch (ClassNotFoundException | SQLException e) {
  e.printStackTrace();
}

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | autodist | | +--------------------+ 2 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/autodist?"
              + "user=root&password=1234");

PreparedStatement st = conn.prepareStatement("select * from <tablename>");

然后:

ResultSet rs = st.executeQuery();