import java.sql.*;
public class work {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//加载驱动【掌握】
// DriverManager.registerDriver(new Driver());//注册驱动【了解】
// 2.通过DriverManager获取数据库连接Connection
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
// 3.通过连接Connection获取执行语句对象Statement
String sql = "select * from work where id = ? and name = ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
//设置查询MySQL表中的参数
pstmt.setInt(1,1);//mysql第一列对应的id为1的
pstmt.setString(2, "cc");//mysql第二列对应的name为cc的
// 4.通过执行语句对象PreparedStatement去执行SQL
ResultSet rs = pstmt.executeQuery();//executeQuery()执行查询数据库
if (rs.next()){
System.out.println("登陆成功!");
System.out.println(rs.getString(2)+"用户存在!");
}else {
System.out.println("用户不存在!");
}
// 5.释放资源
pstmt.close();
connection.close();
}
}