我試圖運行該代碼,但它給了我無數的錯誤,我怎麼知道我自己的JDBC連接和測試錯誤
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
,當我跑了這個節目,我有這個錯誤
和我選擇的選項號1後,我點擊確定它給了我這個錯誤它給了我這個錯誤
ClassNotFoundException: jdbc:mysql://localhost:3306/
SQLException: No database selected
我使用MySQL,我下載我的連接器/ J,我已經把它的類路徑中,H
C:\Program Files\Java\jdk1.6.0_24\bin;C:\Program Files\Java\jre6\lib\ext
任何幫助嗎?以及如何在eclipse helios中設置默認驅動程序? 編輯:
現在我更新了我的codee我的問題是這樣的
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)
我怎麼會知道我的驅動程序類名稱?所有我知道的是,我下載connectorj添加它的路徑和在java文件夾中 – user962206
@ user962206您的驅動程序的製造商通常會告訴您如何配置/使用它與Java。只要看看文檔/用戶指南。每個數據庫的驅動程序都不相同。對於你的msysql情況,com.mysql.jdbc.Driver肯定是對的。 – oers
好吧,我完成了,但是Database_URL有什麼用?最後我更新了我的問題,請檢查 – user962206