好吧,我變得非常煩躁,因爲我意識到這很容易,但在這一點上,我不知道還有什麼要嘗試。該計劃簡單地應該創建一個存儲硬幣的名稱,數量和價值的硬幣表。我已經開始從命令行啓動程序,但沒有完成這項工作後,我將其更改爲在NetBeans中執行。我不斷收到相同的錯誤信息。我搜索瞭解決方案,並且找不到問題,因爲我的驅動程序類似乎是需要的。我目前使用的驅動程序爲com.mysql.jdbc.Driver。com.mysql.jdbc.Driver嘗試創建數據庫時拋出ClassNotFoundException
public class CoinDataBase {
static String file = "C:\\Users\\Dan\\Desktop\\database.properties.txt";
public static void main(String[] args) throws SQLException, IOException,
ClassNotFoundException {
SimpleDataSource.init(file);
Connection conn = SimpleDataSource.getConnection();
try
{
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE Coin (Name VARCHAR(12),Value "
+ "DECIMAL(5,2),QTY DECIMAL(5,0),Value DECIMAL(5,2)"
+ ",Total DECIMAL(5,2))");
stat.execute("INSERT INTO Coin VALUES('Penny',.01,5,.05)");
stat.execute("INSERT INTO Coin VALUES('Nickel',.05,2,.10)");
stat.execute("INSERT INTO Coin VALUES('Dime',.10,3,.30)");
stat.execute("INSERT INTO Coin VALUES('Quarter',.25,2,.50)");
stat.execute("INSERT INTO Coin VALUES('Half Dollar',.50,3,1.50)");
stat.execute("INSERT INTO Coin VALUES('Dollar',1.00,2,2.00)");
ResultSet result = stat.executeQuery("SELECT * FROM Coin");
result.next();
System.out.println(result.getString("Name"));
}
finally
{
conn.close();
}
}
}
我的第二類...
class SimpleDataSource {
private static String url, username, password;
static void init(String fileName) throws IOException, ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
if (username == null) username = "";
password = props.getProperty("jdbc,password");
if (password == null) password = "";
if (driver != null)
Class.forName(driver);
}
static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, username, password);
}
}
和錯誤消息在全:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at coindatabase.SimpleDataSource.init(SimpleDataSource.java:28)
at coindatabase.CoinDataBase.main(CoinDataBase.java:20)
Java Result: 1
我真的很感激任何幫助。我不知道我哪裏錯了。我可以通過瀏覽NetBeans中的服務來連接和創建數據庫,所以我認爲它不配置錯誤。
確保ojdbc6.jar是在你的項目屬性運行時類路徑:http://netbeans.org/kb/docs/java/project-setup.html – 2013-09-13 21:33:08