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