📄 db.java
字号:
import java.sql.*;
//对数据库test的操作类
public class DB{
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
public DB() {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","123");
}catch(ClassNotFoundException e){
}catch(SQLException e){
}
}
public boolean login(String username,String password){
boolean b = false;
try{
String sql = "select * from bank_user where name=? and password=?";
ps = con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
b = true;
}
else{
b = false;
}
}catch(SQLException e){
System.out.println (e.getMessage());
}
return b;
}
public double queryBalance(String username,String password){
double b =0;
String sql = "select * from bank_user where name=? and password=?";
try{
ps = con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
b = rs.getDouble("balance");
}
}catch(SQLException e){
}
return b;
}
public void getMoney(String username,String password,double amount){
try{
String sql = "update bank_user set balance=balance-? where name=? and password=?";
ps = con.prepareStatement(sql);
ps.setDouble(1,amount);
ps.setString(2,username);
ps.setString(3,password);
ps.executeUpdate();
}catch(SQLException e){
}
}
public void addMoney(String username,String password,double amount){
try{
String sql = "update bank_user set balance=balance+? where name=? and password=?";
ps = con.prepareStatement(sql);
ps.setDouble(1,amount);
ps.setString(2,username);
ps.setString(3,password);
ps.executeUpdate();
}catch(SQLException e){
}
}
public String rework(String username,String repassword)
{
try{
String sql = "update bank_user set password=? where name=?";
ps = con.prepareStatement(sql);
ps.setString(1,repassword);
ps.setString(2,username);
ps.executeUpdate();
}catch(SQLException e){
}
return repassword;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -