我想插入到我的MYSQL數據庫,但我的代碼總是去catch部分(這表示上傳到數據庫失敗)。我究竟做錯了什麼?插入MySQL查詢
public static Connection getAttConnection() throws Exception {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "VB";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "***********";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
return conn;
}
public static void addAttendance(String name, String type, String size,
String path, String last_Mod) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean committed = false;
try {
conn = getAttConnection();
conn.setAutoCommit(false);
String query = "INSERT INTO upload (name, type, size, path, last_Mod) VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1,name);
pstmt.setString(2,type);
pstmt.setString(3,size);
pstmt.setString(4,path);
pstmt.setString(5,last_Mod);
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
committed = true;
System.out.println("Upload Correctly");
} catch (Exception e) {
System.err.println("Uploading to database failed");
}
}
您可以打印堆棧跟蹤而不是顯示自定義消息嗎? – 2013-04-29 13:59:39
嘗試在catch中打印StackTrace以查看錯誤 – 2013-04-29 13:59:47
是,給我一秒 – user2264202 2013-04-29 13:59:53