【问题标题】:Why ‘No database selected’ SQLException? [duplicate]为什么“未选择数据库”SQLException? [复制]
【发布时间】:2009-02-25 05:57:47
【问题描述】:

为什么这个程序在第二次进入 do while 循环时没有执行,为什么它给出异常“Exception java.sql.SQLException: [MySQL][ODBC 5.1 Driver][mysqld-5.0.51a- community-nt]未选择数据库"

//import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Vector;

public class DataBase {

    public void LoadDriver() {

        // Load the JDBC-ODBC bridge driver
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ee) {
            ee.printStackTrace();
        }
    }

    // 2.open a data source name by means of the jdbcodbcdriver.

    static void connect() throws SQLException {

        // Connect to the database
        Connection con = DriverManager.getConnection("jdbc:odbc:MySQL", "root", "admin");
        Statement stmt = con.createStatement();
        // Shut off autocommit
        con.setAutoCommit(false);


        System.out.println("1.Insert 2.Delete 3.Update 4.Select");
        Scanner s = new Scanner(System.in);
        int x;
        x = s.nextInt();

        String query; // SQL select string
        ResultSet rs; // SQL query results
        boolean more; // "more rows found" switch
        String v1, v2; // Temporary storage results

        Vector<Object> results = new Vector<Object>(10);


        if (x == 1) {

            try {
                stmt.executeUpdate("INSERT INTO employee( emp_id,emp_name ) VALUES ( '122','shiva' ) ");
            } catch(Exception e){System.out.println("Exception " +e);e.printStackTrace();}
        }

        if (x == 2) {

            try {
                stmt.executeUpdate("DELETE from employee where emp_id='102' ");
            }catch(Exception e){System.out.println("Exception "+e);e.printStackTrace();} 
        }

        if (x == 3) {

            try {
                stmt
                        .executeUpdate("UPDATE employee SET emp_name = 'madavan' where emp_id='20'; ");
            } catch(Exception e){System.out.println("Exception "+e);e.printStackTrace();} 
        }


        query = "SELECT * FROM employee ";
        try {
            rs = stmt.executeQuery(query);
            // Check to see if any rows were read
            more = rs.next();
            if (!more) {

                System.out.println("No rows found.");
                return;
            }

            // Loop through the rows retrieved from the query
            while (more) {

                v1 = "ID: " + rs.getInt("emp_id");
                v2 = "Name: " + rs.getString("emp_name");

                System.out.println(v1);
                System.out.println(v2);
                System.out.println("");

                results.addElement(v1 + "\n" + v2 + "\n");

                more = rs.next();
            }
            rs.close();

        } catch (SQLException e) {
            System.out.println("" + results.size() + "results where found.");
        } 
        finally{stmt.close();}
    }

    public static void main(String[] args) throws SQLException {
        String str = "y";
        do {
            DataBase s = new DataBase();
            s.LoadDriver();
            DataBase.connect();
        Scanner sc = new Scanner(System.in);
        System.out.println("DO u Want to PROCEED TO QUERY : ");
        str = sc.next();
        } while (str !="n");
    }

}

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    除非您必须使用 jdbc/odbc 驱动程序,否则我将使用直接的 mysql jdbc 驱动程序。你可以从mysql免费下载。

    然后

    public void LoadDriver() {
    
            // Load the JDBC-ODBC bridge driver
            try {
                    Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ee) {
                    ee.printStackTrace();
            }
    }
    
    static void connect() throws SQLException {
    
            // Connect to the database
            Connection con = DriverManager.getConnection("jdbc:mysql:host/databasename", "root", "admin");
            Statement stmt = con.createStatement();
    ...
    

    【讨论】:

      【解决方案2】:

      仅从查看异常.. 我猜您没有指定数据库。 如何在不告诉它从哪个模式中选择的情况下对表进行选择? 这通常在连接字符串中设置..

      【讨论】:

      • 我认为它包含在 ODBC 定义中,因为它正在使用 JdbcOdbcDriver
      • 有趣.. 模式名称是 MySql??
      • 这将是他们计算机上 ODBC 定义的名称
      【解决方案3】:

      ODBC 源是否实际设置为选择数据库?例如。可以通过其他 ODBC 客户端工具访问数据库吗?

      如果您需要在 JDBC 字符串中显式选择一个数据库,您可以使用“database”参数来完成。

      但是在 ODBC 设置中选择数据库会更常见。事实上,正如 Clint 所提到的,使用普通的 MySQL JDBC 驱动程序而不是 ODBC 会更常见。

      while (str !="n")

      That's not how you compare strings in Java.

      【讨论】:

        【解决方案4】:

        找到了一个bug listing at MySQL,它给出了这个错误,但使用了不同的技术。但是,在描述中它表明它与重新授权有关,而不是发送数据库信息,所以也许这也是您在此处遇到的情况。

        有些事情对我来说很奇怪(尽管不知道它们是否会对您的错误产生任何影响)

        • 您只需加载一次驱动程序管理器
        • 您没有关闭连接,因此请关闭它或重构以使用相同的连接。

        也许将这两行移到 do 循环之前

        DataBase s = new DataBase();
        s.LoadDriver();
        

        【讨论】:

          猜你喜欢
          • 2012-03-15
          • 2014-10-31
          • 1970-01-01
          • 1970-01-01
          • 2014-12-17
          • 2016-02-14
          • 1970-01-01
          • 2017-05-20
          • 2012-06-13
          相关资源
          最近更新 更多