注意:数据库中需要有user表(ID,name,password)
package cn.tedu.test;
import java.sql.*;
//测试用户的查询
public class test2 {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url="jdbc:mysql://localhost:3306/mysql?characterEncoding=utf8";
String user="root";
String pwd="root";
Connection conn=DriverManager.getConnection(url,user,pwd);
//3.获取传输器
Statement st=conn.createStatement();
//4.执行sql
String sql="select * from user where name='jack' and password='123'";
ResultSet rs=st.executeQuery(sql);
//5.解析结果集
if(rs.next()){
System.out.println("登陆成功");}
else{
System.out.println("登录失败");
}
}
//6.释放资源
//rs.close();
//st.close();
//conn.close();
}