java.sql.SQLException:没有选择数据库 - 为什么? [英] java.sql.SQLException: No database selected - why?

查看:170
本文介绍了java.sql.SQLException:没有选择数据库 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图学习如何通过Java访问mySQL数据库的最后几天。
我能够加载驱动程序并获得与数据库的连接(至少我是这么认为的,因为我没有在那里得到例外......)


the last days I was trying to learn how to access mySQL databases via Java. I am able to load the driver and get a connection to the database ( at least I think so, since I don't get an exception there..)

代码是:

    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);
             }   
        }
    }

当我执行它时,它说:


驱动加载...
连接成功...

database-ConnectionError:java。 sql.SQLException:[MySQL] [ODBC 5.2(w)驱动程序] [mysqld-5.5.31]没有选择数据库

driver loaded...
Connection successful...
database-ConnectionError: java.sql.SQLException: [MySQL][ODBC 5.2(w) Driver][mysqld-5.5.31]No database selected

我真的不知道这个问题,因为我认为在getConnection过程中选择了数据库....

我试图通过添加这一行来选择数据库:

I really don't know the problem, because I thought the database is selected during the "getConnection" process....
I tried to select a database by adding this line:

    stmt.executeUpdate("use test;");

不幸的是它没有用,因为我有另一个例外,它说我应该检查语法。我不明白,因为在我的命令行中它工作正常...
我不知道是否可以通过Java使用这些类型的命令,所以如果不是,请原谅我的错误。

unfortunately it didn't work because I got another exception which said I should check on the syntax. I don't understand that either because in my commandline it works just fine... I don't know if it is possible to use these type of commands via Java so if it isn't, please forgive my mistake.

我希望你能帮助我,我在搜索过程中没有错过这个解决方案!

I hope you can help me and I didn't miss the solution during my own search!

感谢所有回复并利用他们的时间处理我的问题的人!

Already Thanks to all who reply and use their time on my problems!

ps。如果我忘了指出一些重要的信息(我不认为我这样做)请问:)

ps. If I forgot to point out some important infos ( I don't think i did) please ask:)

编辑:我还试图在运行时创建一个新数据库

edit: I also tried to create a new database during runtime

     stmt.executeUpdate("CREATE DATABASE test;");

这实际上有效,但数据库也不会被选中......

this actually works, but the database won't be selected either...

推荐答案

首先,我正在考虑我的答案,向您展示与MySQL数据库连接的另一种更好的方法,它更容易,更少nu-expected 例外

你需要做一些步骤:

Firstly, I am considering my answer to show you another better way for connection with MySQL Database, it's much easier and less nu-expected Exception(s).
You need to do some steps:


  1. 下载 Connector / J 并将其添加到类路径中(如果您使用的是IDE,则添加 .jar 到图书馆,或者有很多关于 YouTube )。

  2. 在MySQL程序中创建数据库。

  3. 请参阅下面的示例为您演示如何在MySQL上连接和执行查询:

  1. Download Connector/J and add it to your class path(if you are using an IDE there is add the .jar to the library, or there is many tuts on YouTube).
  2. Create your database in your MySQL program.
  3. See this example below example below I made for you demonstrates how to connect and execute queries on MySQL :

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();
  }
}


这篇关于java.sql.SQLException:没有选择数据库 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆