0

I tried to run this code but it gave me numerous errors, and How do I know my Own

 import javax.swing.JOptionPane; import java.sql.*; public class JdbcConnection{ static String userid="root", password = "123192"; static String url = "jdbc:mysql://localhost:3306/Testdb"; // String url = "jdbc:mySubprotocol:myDataSource"; ? static Statement stmt; static Connection con; public static void main(String args[]){ JOptionPane.showMessageDialog(null,"JDBC Programming showing Creation of Table's"); int choice = -1; do{ choice = getChoice(); if (choice != 0){ getSelected(choice); } } while ( choice != 0); System.exit(0); } public static int getChoice() { String choice; int ch; choice = JOptionPane.showInputDialog(null, "1. Create Employees Table\n"+ "2. Create Products Table\n"+ "0. Exit\n\n"+ "Enter your choice"); ch = Integer.parseInt(choice); return ch; } public static void getSelected(int choice){ if(choice==1){ createEmployees(); } if(choice==2){ createOrders(); } } public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); //Class.forName("myDriver.ClassName"); ? } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, userid, password); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } return con; } /*CREATE TABLE Employees ( Employee_ID INTEGER, Name VARCHAR(30) );*/ public static void createEmployees() { Connection con = getConnection(); String createString; createString = "create table Employees (" + "Employee_ID INTEGER, " + "Name VARCHAR(30))"; try { stmt = con.createStatement(); stmt.executeUpdate(createString); stmt.close(); con.close(); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } JOptionPane.showMessageDialog(null,"Employees Table Created"); } /*CREATE TABLE Orders ( Prod_ID INTEGER, ProductName VARCHAR(20), Employee_ID INTEGER );*/ public static void createOrders() { Connection con = getConnection(); String createString; createString = "create table Orders (" + "Prod_ID INTEGER, " + "ProductName VARCHAR(20), "+ "Employee_ID INTEGER )"; try { stmt = con.createStatement(); stmt.executeUpdate(createString); stmt.close(); con.close(); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } JOptionPane.showMessageDialog(null,"Orders Table Created"); } }//End of class 

and I had this error

when I ran this program and I selected option number 1 it gave me this error

ClassNotFoundException: jdbc:mysql://localhost:3306/ SQLException: No database selected 

after I click ok it gave me this error

I am using MySQL, I've downloaded my connector/j and I've set it's classpath, h

C:\Program Files\Java\jdk1.6.0_24\bin;C:\Program Files\Java\jre6\lib\ext 

Any help? and how do I set the default driver in eclipse helios? EDIT:

I updated the my codee my problem now is this

Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at JdbcConnection.getChoice(JdbcConnection.java:33) at JdbcConnection.main(JdbcConnection.java:15) 

2 Answers 2

3

This:

Class.forName("jdbc:mysql://localhost:3306/"); 

is wrong. Like your source-code comment suggested it needs to be:

Class.forName("myDriver.ClassName"); 

And using mysql it probably is:

Class.forName("com.mysql.jdbc.Driver"); 

Your url is used to create a connection with:

con = DriverManager.getConnection(url, userid, password); 

Class.forName loads a driver class. It is not responsible for making the connection.

By the way: You get two errors, because you don't exit your method on the ClassNotFoundError, which is quite a heave error and your method should not be allowed to execute further.
Therefore your program will execute further and throw the second error.

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

5 Comments

how will I know my driver class name? all I know is that I downloaded connectorj added it the path and in the java folder
@user962206 The manufacturer of your driver usually tells you how to configure/use it with java. Just take a look at the documentation / user guides. The driver is different for each database. For your msysql case com.mysql.jdbc.Driver is certainly right.
alright, I am done, but what's the use of Database_URL?? and lastly I updated my question please check
@user962206 The URL is used to create a conenction with DriverManager.getConnection. Your new problem indicates, that the value in choice cannot be transformed into an Integer. It could contain characters. Anyhow it is preferable to open a new Question, if you face a new Problem and accept an answer(if any answer was helpful) here.
the error message says java.lang.NumberFormatException: null , that's because JOptionPane.showInputDialog returns null. And this is really a completely different question :)
1

Class.forName is for loading the driver class

it should be something like this

Class.forName("com.mysql.jdbc.Driver"); 

and your url should be like this

url ="jdbc:mysql://localhost:3306/yourDataBaseName" 

6 Comments

I haven't created any databases yet
I don't know to type queries in a cmd
Well I think you need to have a database already created, only then you can get Connection to that database. If you don't have database then how can you get connected to that specific.
you are creating tables without selecting any database will definitely give you error. In MySql you need to have a database to create table into.
OKay , I am done fixing my Database Url, my Driver. now I updated my question, I have lesser errors,kindly reread my code for new errors , I updated it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.