1

So the purpose of this database is to execute the queries needed for a website that will be accessing this, here is the code:

package dodRus;
//STEP 1. Import required packages
import java.sql.*;

public class Connect_Database {
 // JDBC driver name and database URL
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/?user=root";

 //  Database credentials
 static final String USER = "root";
 static final String PASS = ;

 public static void main(String[] args) {
 Connection conn = null;
 Statement stmt = null;
 try{
    //STEP 2: Register JDBC driver
   Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(DB_URL,USER,PASS);

   //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "INSERT INTO Inventory " + "VALUES (shirt,1234,5)";
    stmt.executeUpdate(sql);


 }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
 }catch(Exception e){
//Handle errors for Class.forName
    e.printStackTrace();
 }finally{
    //finally block used to close resources
    try{
       if(stmt!=null)
          stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
       if(conn!=null)
          conn.close();
    }catch(SQLException se){
       se.printStackTrace();
    }//end finally try
  }//end try

}
 }

and here is the error:

java.sql.SQLException: No database selected
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3020)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1074)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1008)
    at dodRus.Connect_Database.main(Connect_Database.java:30)

Obviously it is not making a successful connection to the database but I can't figure out why

3
  • 2
    I really hope you did NOT post the real root password in your example code... Commented Apr 8, 2016 at 0:56
  • 1
    Even if it's localhost, still not a good idea to post it Commented Apr 8, 2016 at 0:57
  • 1
    Anyways you never specified the database name you are going to use in your url. Error says it right there. url should be jdbc:mysql://127.0.0.1:3306/(database name here)?user=root Commented Apr 8, 2016 at 1:00

1 Answer 1

2

You need to include the database name in your DSN string.

static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/<name_of_database>?user=" + USER + "&password=" + PASS;

See the Connector/J documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.