java.sql.SQLException: No database selected - why?

和自甴很熟 提交于 2019-11-27 15:35:48
Azad

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

Before you can add a table, you first have to select a database. you can create a new database with:

  CREATE DATABASE database_name

you can connect to a specific database with:

String url = "jdbc:mysql://localhost/databasename";
String username = "test";
String password = "test";
Connection connection = DriverManager.getConnection(url, username, password);

Here is your updated example, which works for me.

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

Make sure you have added a proper mysql-connector to your build path. I used the: mysql-connector-java-5.1.24-bin.jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!