我已經使用了jdbc驅動程序,但對於這段程序我無法連接到db.This不會拋出任何異常或任何東西。只是不會連接。我不能在網上找到一個解決方案either.Below是代碼我試圖運行:(請在解決這個幫助預先感謝您:)爲什麼我不能使用java連接到mysql db
public class HeapMySql<T extends Comparable<T>> implements HeapInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/Heap";
static final String USERNAME = "root";
static final String PASSWORD = "";
private int size = 0 ;
String sql;
static Statement stmt = null;
static Connection conn = null;
static ResultSet rs = null;
public void HeapMySql(){
try
{
sql = "CREATE TABLE testHeap (index integer, value integer);";
stmt.executeUpdate(sql);
System.out.println("Done");
}catch(Exception e){
}
}
public static void main(String [] arg){
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
System.out.println("Connected database successfully...");
System.out.println("Creating table in given database..."); //lets create a table in our database
stmt = conn.createStatement();
HeapMySql test1 = new HeapMySql<>();
}catch(ClassNotFoundException | SQLException ex){
}finally{
}
如果沒有異常發現,這意味着它的連接 – nafas 2015-03-13 16:38:27
你怎麼能說它不拋出任何錯誤你還沒有寫ex.printstachtrace – SpringLearner 2015-03-13 16:38:41
在'抓?(ClassNotFoundException的|的SQLException前)'塊把'ex.printStackTrace() ;'。 – Oli 2015-03-13 16:41:44