ublic boolean login(string username,string password) throw SQLException{
Connection connection = DBUtil.getconnection();
string sql=“select * from tb_user where username=? and password=?”;
Preparedstatement statement=connection. Preparedstatement (sql);
statement.setString(parameterindex:1,username);
statement.setString(parameterindex:2,password);
Resultset resultset=statement.executeQuery();
if(resultset.next()){
return true;
}else
return false;
ublic boolean zhuce(string username,string password) throws SQLException{
Connection connection=DBUtil.getconnection();
string sql=“insert into tb_user(username,password) values(?,?)”;
preparedStatement statement=connection.prepareStatement(aql);
int i=statement.executeUpdate();
if(i!=0){
return true;
}else
return false;
}
不完整版
新建类user
public class user{
private int id;
private string username;
private string password;
public int getid(){
return id;
}
public void setid(int id){
this;
public class DBUtill{
public static connection getconnection(){
class.forname(“com.mysql.jdbc.drivrr”);
Connection connection=drivermansger.getConnection(url:”……”)
string sql=“select * from tb_user”;
preparedStatement preparedStatement.executeQuery();
while(results.next)
}
public static void
try{
connection=DBUtil.getconnection();
string sql=“select * from tb_user”;
preparedStatement=connection. preparedStatement
public class userdao{
public boolean login(String username,String password) throws SQLException{
Connection connection =DBUtil.getconnection ();
string sql=“select * from tb_user where username=? and password=?”;
PreparedStatement=connection.prepareStatement(sql);
statement.setString(parameter index:1,username);
statement.setString(parameter index:2,password);
ResultSet resultSet=statement.executeQuery();
if(resultSet.next()){
return true;
}else
return false;
}
}
登入注册
system.out.println(“登入失败”);
system.out.println(“请先注册”);
system.out.println(“请输入注册名”);
string zcm=input.next();
system.out.println(“请输入注册密码”);
string zcmm1=input.next();
system.out.println(“请确认密码”);
string zcmm2=input.next();
if(zcmm1==zcmm2){
userDao.zhuce(username,password);
}else{
system.out.println(“密码有误,请重新”);
}
}