JDBC流程
1.加载驱动(cj为驱动8的特性)
Class.forName("com.mysql.cj.jdbc.Driver");
2.获得连接对象(驱动8需要加时区)
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/(需连接数据库名称)?serverTimezone=UTC","root",password:"xxxxx");
3.获得PrepareStatement对象 预编译sql语句
PrepareStatement prepareStatement=connection.prepareStatement(""select *from users where username=? and password=?"");
//为?占位符赋值
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
4.执行sql语句并接收结果
ResultSet resultSet=prepareStatement.executeQuery();
5.处理结果
if(resultSet.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
6.释放资源(先开后关)
Result.close();
PrepareStatement.close();
Connection.close();