1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
2.创建连接
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbName?characterEncoding=utf-8”);
3.创建语句
PreparedStatement ps = con.prepareStatement(“select * from user where age between ? and ?”);
ps.setInt(1,1000);
ps.setInt(2,1000);
4.执行语句
ResultSet rs = ps.executeQuery();
5.处理结果集
while(rs.next()){
System.out.println(rs.getInt(“age”) + " - " + rs.getString(“name”));
}
6.关闭资源
finally {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}