java连接mySQL后存入用户名和密码,然后登陆时实行验证
答案:1 悬赏:40 手机版
解决时间 2021-01-27 12:36
- 提问者网友:绫月
- 2021-01-26 13:30
java连接mySQL后存入用户名和密码,然后登陆时实行验证
最佳答案
- 五星知识达人网友:一把行者刀
- 2021-01-26 14:02
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
String user = "root" ,password = "mysql",url="jdbc:mysql://localhost:3306/test??characterEncoding=utf8";
Connection connection = DriverManager.getConnection(url, user, password);
//添加用户
String sql = "insert into t1(id,name,password) values(?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, "123123123");
statement.setString(2, "jdbc");
statement.setString(3, "password");
System.out.println(statement.executeUpdate());
//登录
sql = "select * from t1 where name = ? and password = ?";
PreparedStatement query = connection.prepareStatement(sql);
query.setString(1, "jdbc");
query.setString(2, "jdbc");
ResultSet rs = query.executeQuery();
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
connection.close();
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
String user = "root" ,password = "mysql",url="jdbc:mysql://localhost:3306/test??characterEncoding=utf8";
Connection connection = DriverManager.getConnection(url, user, password);
//添加用户
String sql = "insert into t1(id,name,password) values(?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, "123123123");
statement.setString(2, "jdbc");
statement.setString(3, "password");
System.out.println(statement.executeUpdate());
//登录
sql = "select * from t1 where name = ? and password = ?";
PreparedStatement query = connection.prepareStatement(sql);
query.setString(1, "jdbc");
query.setString(2, "jdbc");
ResultSet rs = query.executeQuery();
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
connection.close();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯