JDBC for mysql

1173 단어 sqlmysqljdbc
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();
}
}

좋은 웹페이지 즐겨찾기