1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
2.建立连接
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
Connection conn=DriverManager.getConnection(url,user,password);
3.创建statement对象
Statement stmt=conn.createStatement();
4.执行sql
String sql=" select * from user";
ResultSet rs=stmt.executeQuery(sql);
5.处理结果集
while(rs.next()){
//获取id
int id=rs.getInt("id");
//获取username
String username=rs.getString("username");
}
6.关闭连接,释放资源
rs.close();
stmt.close();
conn.close();