0
我想知道在按下按鈕(鍵綁定)完成使用後,如何關閉數據庫連接。如何使用按鈕關閉數據庫連接
這裏就是我的了:
public static void main(String[] argv) {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test14", "root", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("Connection to DB Success!");
} else {
System.out.println("Failed to make connection!");
}
System.out.println("\n" + "----------- Database Results ------------" + "\n");
try {
Statement stmt = connection.createStatement();
String sql = "SELECT * FROM first_table";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id_col = rs.getInt("id");
String name = rs.getString("name");
String address = rs.getString("address");
String p = id_col + " " + name + " " + address;
System.out.println(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我必須在我的查詢語句後的密切聯繫增加?
希望對此有所幫助。
? –
在完成我的查詢後,基本上可以選擇關閉數據庫連接。我只是玩弄一些jdbc連接和java代碼,看看它是如何工作的。 – Jeiman