我試圖連接到我的筆記本電腦上的mysql。使用我粘貼在下面的代碼。我已經添加包含文件的完整路徑CLASSPATH:在測試所有建議的解決方案時無法連接到jdbc
mysql-connector-java-5.1.37-bin.jar
,並出口它。但是我得到持續的提到的錯誤。有人可以告訴我,我錯過了什麼讓我從我的機器上得到這個錯誤信息。我試圖執行用戶下的代碼,而不是root,這是我正在使用的代碼,當然,靜態最終變量「用戶」和「密碼」不是用戶名和密碼是不是用戶名和密碼不是空字符串,因爲他們在這裏:
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// Database credentials
static final String USER = "";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connect ion
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();
String sql = "CREATE DATABASE STUDENTS";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
我真的試圖包括任何相關信息,我可以猜測它是相關的。如果需要更多信息來回答我願意提供的問題。
什麼是實際的錯誤信息。標題看起來像是手工輸入 – Drew
數據庫URL需要包含數據庫名稱。爲什麼所有的空白行?自2007年以來,第2步就不必要了。關閉連接關閉語句和結果集。 – EJP
這是實際的錯誤消息:拋出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver \t在java.net.URLClassLoader.findClass(URLClassLoader.java:381) \t在java.lang.ClassLoader.loadClass(類加載器的.java:424) \t在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) \t在java.lang.ClassLoader.loadClass(ClassLoader.java:357) \t在java.lang.Class中。 (JDBCExample.java:19) 再見! –