[code="java"][/code]String url = "jdbc:mysql://localhost:3306/t_test_db?user=root&password=root";
String sql = "select * from t_test";
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
int a = rs.getInt("id");
String b = rs.getString("name");
System.out.println(" id = " + a + "; name = " + b);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if(stm != null){
stm.close();
stm = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
String sql = "select * from t_test";
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
int a = rs.getInt("id");
String b = rs.getString("name");
System.out.println(" id = " + a + "; name = " + b);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if(stm != null){
stm.close();
stm = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}