package Login1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTextField;
public class operation {
JTextField userNameT;
JTextField passwordT;
private boolean login=true;//默认login true为登录
public void operate() {
String className="com.mysql.jdbc.Driver";
try {
Class.forName(className);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//2.创建数据库连接
String url="jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC";
String DBuser="root";
String name=userNameT.getText().trim();
String password=passwordT.getText().trim();
String DBpassword="root1";
try {
Connection connection=DriverManager.getConnection(url, DBuser, DBpassword);
//3.构建查询器
Statement statement=connection.createStatement();
//4.1创建SQL语句
ResultSet resultSet = null;
String sql="select * from login where ";
sql+="userName= '"+name+"'";
//4.2执行SQL语句
resultSet=statement.executeQuery(sql);
resultSet.beforeFirst();
if(login)
{
if(resultSet.next())
{
String pw=resultSet.getString("password");
if(pw.equals(password))
{
System.out.println("登录成功");
}
else {
System.out.println("密码错误");
}
}
else {
System.out.println("该用户名不存在");
}
}
else {
if(!resultSet.next())
{
System.out.println("该用户名已存在");
return ;
}
sql="insert into login values('"+name+"','"+password+"')";
statement.execute(sql);
}
//5.关闭数据库
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public operation(JTextField jt1,JTextField jt2) {
// TODO Auto-generated constructor stub
userNameT=jt1;
passwordT=jt2;
}
}
注册与登录
最新推荐文章于 2024-03-09 13:45:00 发布