实现带条件查询
作者:互联网
///////////////////
接下来我们重新在原来之前数据库中创建一个表
mysql> CREATE TABLE admin(
-> id int auto_increment,
-> loginpwd varchar(20) not null,
-> loginid varchar(30) not null,
-> PRIMARY KEY ( id )
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
在插入数据
insert admin(loginpwd,loginid) value('admin','admin'),('jack','123456');
最终结果
mysql> select * from admin;
+----+----------+---------+
| id | loginpwd | loginid |
+----+----------+---------+
| 1 | admin | admin |
| 2 | jack | 123456 |
+----+----------+---------+
你的登录名叫admin,密码为admin,或者你的另一个登录名叫123456,密码为jack
接下来我们查询数据
select * from admin where loginid='admin' and loginpwd='admin';
/////////////////
接下来我们在运行下面这段java代码
它可以检查你输入的登录名和密码与库中的是否匹配
package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class MySQL1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("用户名:");
String loginid = input.next();
System.out.print("密 码:");
String loginpwd = input.next();
String sql = "select * from admin where loginid='"+loginid+"' and loginpwd='"+loginpwd+"';";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC","root", "");
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
rs.close();
ps.close();
connection.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
标签:java,实现,sql,查询,admin,条件,import,loginpwd,loginid 来源: https://blog.csdn.net/qq_43776408/article/details/100176211