public class JdbcDemo {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement psmt = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String sql = "select * from account where id =?";
psmt = connection.prepareStatement(sql);
psmt.setInt(1,2);
resultSet = psmt.executeQuery();
while (resultSet.next()) {
System.out.println("用户id:"+resultSet.getInt("id")+",用户名称:"+resultSet.getString("name"));
}
} catch (Exception e ) {
System.out.println("发现异常错误 :");
e.printStackTrace();
}finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (psmt != null) {
psmt.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}