Howdy,
I am trying to write a simple program in java that creates a database with some tables and adds some data, but I keep gettin this error:

java.sql.SQLException: No database selected

I am posting the code below, it's really easy to understand.
Please HELP!!! I am new at mySQL & java in general.

import java.sql.*;
import java.util.Scanner;

public class Testingdb 
 {
     public static void main() throws Exception
     {
        String dbname = "stud";
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");
            Statement stmt = con.createStatement();
            stmt.executeUpdate("DROP DATABASE " + dbname);
            stmt.executeUpdate("CREATE DATABASE " + dbname);
        
            // Krijimi i tabelave
        
            stmt.executeUpdate("CREATE TABLE stud (ID int NOT NULL, " + 
                           " FirstName char(30) NOT NULL, " + 
                           " LastName char(30) NOT NULL, " + 
                           " Age int NOT NULL)");
                     
                           for (int i = 1; i < 10; i++)
                           {
                               Scanner scan = new Scanner(System.in);
                               System.out.print("Jep Emrin: ");
                               String emri = scan.nextLine();
                               System.out.println();
                               System.out.print("Jep Mbiemrin: ");
                               String mbiemri = scan.nextLine();
                               System.out.println();
                               System.out.print("Jep Moshen: ");
                               int mosha = scan.nextInt();
                               stmt.executeUpdate("INSERT INTO stud (ID,FirstName,LastName,Age) VALUES (i,emri,mbiemri,mosha)");
                            }
                          
        
                            stmt.close();
                            con.close();
           }
           catch(Exception e)
           {
               System.out.print(e);
            }
                        
      }
}

I assume you are getting the SQLException on the third line.
To create the table you will need to switch to the database you just created by using the use db-name statement or else while creating the table itself in the create statement you will need to specify the name of the table as CREATE TABLE `db-name`.`table-name` .

Your problem, is that with a JDBC connection

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");

You must provide a db instance. Since you're connecting with root, anyway, use "mysql". But, I would advise against using this, and against using root.

Connect to test.

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");

I would strongly advise against doing any of this, though. Create the "base" DB once, localy, on your development machine, the way you want to see it by the client, then mysqldump that DB, then modify this dump file and add the sql commands necessary for creating the application user and granting it it's rights. Then have the client simply execut this file with the mysql command with a redirect indut (or, if you absolutely must have the app do it, then use a runtime exec).

hey there, thank you for your help.
I had to use the USE query to make it work :)
thnx for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.