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

查看:71
本文介绍了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);
             }   
        }
    }

当我执行它时,它说:

驱动已加载...
连接成功...
数据库连接错误: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;");

在创建声明之后.

after creating the Statement.

不幸的是它没有工作,因为我遇到了另一个异常,它说我应该检查语法.我也不明白,因为在我的命令行中它工作得很好......我不知道是否可以通过 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 Exception(s).
您需要执行一些步骤:

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:
"+ex.toString());
       ex.printStackTrace();

    } catch(SQLException ex) {
        System.out.println("SQLException:
"+ex.toString());
        ex.printStackTrace();
    }
  }

  public static void main(String...args) {
    new MySqlConnection();
  }
}

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

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