一个简单的小型应用,用于模拟银行存取。
基于java多线程,GUI、操作数据库,客服端服务端。
代码在,其中yinhan目录为此代码。javaee课程: javaEE作业练习 - Gitee.com https://gitee.com/xubb6b/xbb/tree/master/yinhan
GUI界面的代码:
登录界面
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.shujuku.*;
import javax.swing.*;
public class DengluActionEvent extends JFrame implements ActionListener{
JTextField inputText1;
JPasswordField inputText2;
JPanel p1,p2,p3;
JLabel title_1,user_1,password_1;
JButton button1,button2;
JButton button3 = new JButton("注销账户");
JButton button4 = new JButton("管理登录");
public DengluActionEvent() {
inputText1= new JTextField(10);
inputText2 = new JPasswordField();
title_1 = new JLabel("银行系统");
button1 = new JButton("登录");
button2= new JButton("注册");
user_1= new JLabel("用户名");
password_1 = new JLabel("密码");
inputText2.setEchoChar('*');
user_1.setBounds(60,100,60,35);
password_1.setBounds(60,150,60,35);
inputText1.setBounds(111,100,200,40);
inputText2.setBounds(111,150,200,40);
button1.setBounds(120,210,75,30);
button2.setBounds(210,210,75,30);
button3.setBounds(100,260,90,30);
button4.setBounds(210,260,90,30);
title_1.setBounds(150,50,150,30);
title_1.setFont(new Font("宋体",Font.BOLD,18));
setBounds(100,100,460,360);
setResizable(true);
setLayout(null);
setVisible(true);
add(title_1);
add(inputText1);
add(inputText2);
add(password_1);
add(user_1);
add(button2);
add(button1);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
isLogin();
//JOptionPane.showMessageDialog(this, "登录成功");
}
else if(e.getSource()==button2) {
new RegisterActionEvent();
this.setVisible(false);
}
if(e.getSource()==button3) {
new DeleteUser();
dispose();
}
if(e.getSource()==button4) {
if((inputText1.getText().equals("0"))&&(inputText2.getText().equals("0"))){ //管理登录
new ContralUser();
dispose();
}
else {
JOptionPane.showMessageDialog(this, "密码错误!!!");
}
}
}
void isLogin(){
SelectShujuku database=new SelectShujuku();
String LoginID =inputText1.getText();
String password=new String(inputText2.getPassword());
String userID=database.getDatabySql("select id from user where id="+LoginID,"id"); //从数据库导出登录名
String pswdDatabase=String.valueOf(database.getDatabySql(("select password from user where id="+userID ),"password")); //从数据库导出密码
if(password.equals(pswdDatabase)) {
new HomepageActionEvent(userID).setVisible(true);
dispose();
}
else {
JOptionPane.showMessageDialog(this, "密码错误!!!");
inputText1.setText("");
inputText2.setText("");
}
}
}
主页
package com.Gui;
import com.shujuku.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HomepageActionEvent extends JFrame implements ActionListener{
JButton button1, button2, button3, button4;
JLabel title,balance,yuan;;
JTextField inputField;
public HomepageActionEvent (String userid) {
setTitle("银行主页");
title = new JLabel("银行主页");
button1 =new JButton("取款");
button2 =new JButton("存款");
button3 =new JButton("转账");
button4 =new JButton("退出");
balance = new JLabel("你的余额为:");
yuan= new JLabel("元");
inputField = new JTextField();
title.setFont(new Font("宋体",Font.BOLD,18));
title.setBounds(200,10,100,30);
balance.setBounds(150,150,100,30);
button1.setBounds(0,50,60,35);
button2.setBounds(423,50,60,35);
button3.setBounds(0,300,60,35);
button4.setBounds(423,300,60,35);
inputField.setBounds(220,150,100,30);
yuan.setBounds(320,150,100,30);
setBounds(200,100,500,400);
setResizable(false);
setLayout(null);
setVisible(true);
add(title);
add(button1);
add(button2);
add(button3);
add(button4);
add(balance);
add(inputField);
add(yuan);
button1.addActionListener(new ActionListener() { //取钱
public void actionPerformed(ActionEvent e) {
new GetMoneyActionEvent(userid);
dispose();
}
});
button2.addActionListener(new ActionListener() { //存钱
public void actionPerformed(ActionEvent e) {
new CunqianActionEvent(userid);
dispose();
}
});
button3.addActionListener(new ActionListener() { //存钱
public void actionPerformed(ActionEvent e) {
new ZhuanzhangActionEvent(userid);
dispose();
}
});
button4.addActionListener(new ActionListener() { //取钱
public void actionPerformed(ActionEvent e) {
dispose();
}
});
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//显示框读出数据
SelectShujuku database=new SelectShujuku(); //新建数据库对象
String mymoney = database.getDatabySql("select mymoney from user where id= "+userid,"mymoney");
inputField.setText(mymoney);
}
public void actionPerformed(ActionEvent e) {
}
}
存钱
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.shujuku.*;
public class CunqianActionEvent extends JFrame implements ActionListener{
JLabel title = new JLabel("正在存钱");
JLabel label = new JLabel("请输入存钱金额");
JLabel yuan= new JLabel("元");
JButton button1 = new JButton("确定");
JButton button2 = new JButton("取消");
JTextField inputField = new JTextField(10);
public CunqianActionEvent(String userid) {
label.setBounds(100,150,100,35);
inputField.setBounds(200,150,100,35);
title.setBounds(160,50,101,35);
title.setFont(new Font("宋体",Font.BOLD,18));
yuan.setBounds(300,150,100,35);
button1.setBounds(120,200,100,35);
button2.setBounds(250,200,100,35);
setBounds(200,100,500,400);
setResizable(false);
setLayout(null);
setVisible(true);
add(yuan);
add(inputField);
add(label);
add(title);
add(button1);
add(button2);
// button1.addActionListener(this);
button1.addActionListener(new ActionListener() { //取钱
public void actionPerformed(ActionEvent e) {
String getmoney = inputField.getText();
UpdateShujuku updata = new UpdateShujuku();
String sql = "update user set mymoney=mymoney+"+getmoney+" where id="+userid;
updata.Updata(sql);
//JOptionPane.showMessageDialog(this, "存款成功");
dispose();
new HomepageActionEvent(userid);
}
});
button2.addActionListener(new ActionListener() { //取消按钮
public void actionPerformed(ActionEvent e) {
new HomepageActionEvent(userid);
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
}
}
取钱
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.shujuku.SelectShujuku;
import com.shujuku.UpdateShujuku;
public class GetMoneyActionEvent extends JFrame implements ActionListener{
JLabel label = new JLabel("请输入取款金额:");
JLabel label2 = new JLabel("正在取钱");
JLabel yuan= new JLabel("元");
JButton button1 = new JButton("确定");
JButton button2 = new JButton("取消");
JTextField inputField = new JTextField(10);
public GetMoneyActionEvent(String userid) {
label.setBounds(100,150,100,35);
inputField.setBounds(200,150,100,35);
label2.setBounds(160,50,101,35);
label2.setFont(new Font("宋体",Font.BOLD,18));
yuan.setBounds(300,150,100,35);
button1.setBounds(120,200,100,35);
button2.setBounds(250,200,100,35);
setBounds(200,100,500,400);
setResizable(false);
setLayout(null);
setVisible(true);
add(yuan);
add(inputField);
add(label);
add(label2);
add(button1);
add(button2);
button1.addActionListener(new ActionListener() { //取钱
public void actionPerformed(ActionEvent e) {
String jianmoney = inputField.getText();
double get = Double.valueOf(jianmoney);
UpdateShujuku updata = new UpdateShujuku();
SelectShujuku database = new SelectShujuku();
String mymoney = database.getDatabySql("select mymoney from user where id=" + userid, "mymoney");
double yuanlai = Double.valueOf(mymoney);
if(yuanlai<get) {
JOptionPane.showMessageDialog(null, "余额不足!", "出错", JOptionPane.ERROR_MESSAGE);
}else {
String sql = "update user set mymoney=mymoney-"+jianmoney+" where id="+userid;
updata.Updata(sql);
//JOptionPane.showMessageDialog(null, "存款成功");
dispose();
new HomepageActionEvent(userid);
}
}
});
button2.addActionListener(new ActionListener() { //取消按钮
public void actionPerformed(ActionEvent e) {
new HomepageActionEvent(userid);
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
}
}
注册
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.shujuku.*;
public class RegisterActionEvent extends JFrame implements ActionListener{
JTextField inputText1,inputText3,inputText4,inputText5;
JPasswordField inputText2;
JPanel p1,p2,p3;
JTextArea textShow;
JLabel title,user,password,id,shenfen,name;
JComboBox root_1 =new JComboBox();
JButton button1;
public RegisterActionEvent() {
setTitle("注册银行账户");
inputText1= new JTextField(10);
inputText2 = new JPasswordField(6);
inputText3= new JTextField(10);
inputText4= new JTextField(10);
inputText5= new JTextField(10);
title = new JLabel("注册银行账号");
button1 = new JButton("注册");
user= new JLabel("用户名");
name= new JLabel("姓名");
title = new JLabel("注册银行账号");
password = new JLabel("密码");
id = new JLabel("身份证号码");
shenfen = new JLabel("注册类型");
root_1.addItem("用户");
root_1.addItem("管理员");
inputText2.setEchoChar('*');
name.setBounds(60,150,60,35);//姓名
user.setBounds(60,100,60,35);//id
password.setBounds(60,200,60,35);//密码
id.setBounds(20,250,80,35);//身份证号
shenfen.setBounds(50,300,60,35);//类型
inputText1.setBounds(111,100,200,40);//id
inputText2.setBounds(111,200,200,40);//密码
inputText3.setBounds(111,150,200,40);//姓名
inputText4.setBounds(111,250,200,40);//身份证号码
//inputText5.setBounds(105,200,200,40);
root_1.setBounds(111,300,200,40);
button1.setBounds(150,350,75,30);
title.setBounds(150,50,150,30);
title.setFont(new Font("宋体",Font.BOLD,18));
setBounds(200,100,500,600);
setResizable(true);
setLayout(null);
setVisible(true);
add(title);
add(inputText1);
add(inputText2);
add(inputText3);
add(inputText4);
add(name);
add(password);
add(user);
add(shenfen);
add(button1);
add(id);
add(root_1);
button1.addActionListener(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
String sid = inputText1.getText();
String spassword = inputText2.getText();
String sname = inputText3.getText();
String sshenfen = inputText4.getText();
String monney = "0";
SelectShujuku database=new SelectShujuku();
String LoginID =inputText1.getText();//查询是否有相同的用户名
String userID=database.getDatabySql("select id from user where id="+LoginID,"id"); //从数据库导出登录名
if(LoginID.equals(userID)) {
JOptionPane.showMessageDialog(this, "注册失败,有相同用户名");
return ;
}
InsertShujuku inser = new InsertShujuku();
String sql1 = "insert into user(id,password,name,shenfen,mymoney)"+"value('"+sid+"','"+spassword+"','"+sname+"','"+sshenfen+"','"+monney+"')";
inser.Insert(sql1);
JOptionPane.showMessageDialog(this, "注册成功");
new DengluActionEvent();
}
}
}
转账
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.shujuku.SelectShujuku;
import com.shujuku.UpdateShujuku;
public class ZhuanzhangActionEvent extends JFrame implements ActionListener {
JLabel title = new JLabel("正在转账");
JLabel label1 = new JLabel("请输入转账账号");
JLabel label2 = new JLabel("请输入转账金额");
JLabel yuan = new JLabel("元");
JButton button1 = new JButton("确定");
JButton button2 = new JButton("取消");
JTextField inputField1 = new JTextField(10);
JTextField inputField2 = new JTextField(10);
public ZhuanzhangActionEvent(String userid) {
label1.setBounds(100, 100, 100, 35);
label2.setBounds(100, 150, 100, 35);
inputField1.setBounds(200, 100, 100, 35);
inputField2.setBounds(200, 150, 100, 35);
title.setBounds(160, 50, 101, 35);
title.setFont(new Font("宋体", Font.BOLD, 18));
yuan.setBounds(300, 150, 100, 35);
button1.setBounds(120, 200, 100, 35);
button2.setBounds(250, 200, 100, 35);
setBounds(200, 100, 500, 400);
setResizable(false);
setLayout(null);
setVisible(true);
add(yuan);
add(inputField1);
add(inputField2);
add(label1);
add(label2);
add(title);
add(button1);
add(button2);
// 监听两个按钮
button1.addActionListener(new ActionListener() { // 确定按钮
public void actionPerformed(ActionEvent e) {
UpdateShujuku updata = new UpdateShujuku();
SelectShujuku database = new SelectShujuku();
String snum = inputField1.getText();
if (snum.equals(userid)) {
JOptionPane.showMessageDialog(null, "不能给自己转账", "出错", JOptionPane.ERROR_MESSAGE);
} else {
String sentmoney = inputField2.getText();
double sent = Double.valueOf(sentmoney);
String mymoney = database.getDatabySql("select mymoney from user where id=" + userid, "mymoney");
double yuanlai = Double.valueOf(mymoney);
if (sent > yuanlai) {
JOptionPane.showMessageDialog(null, "余额不足!", "出错", JOptionPane.ERROR_MESSAGE);
} else {
String sql1 = "update user set mymoney=mymoney-" + sentmoney + " where id=" + userid;// 自己的
updata.Updata(sql1);
String sql2 = "update user set mymoney=mymoney+" + sentmoney + " where id=" + snum;// 转给用户的id
updata.Updata(sql2);
new HomepageActionEvent(userid);
dispose();
}
}
}
});
button2.addActionListener(new ActionListener() {// 退出按钮
public void actionPerformed(ActionEvent e) {
new HomepageActionEvent(userid);
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
}
}
删除用户
package com.Gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.shujuku.*;
public class DeleteUser extends JFrame implements ActionListener {
JLabel title = new JLabel("正在注销用户");
JLabel deteleuserid = new JLabel("请输入注销的用户id:");
JLabel deteleuserpassword = new JLabel("请输入注销的用户密码:");
JTextField inputField1 = new JTextField(10);
JTextField inputField2 = new JTextField(10);
JButton button1= new JButton("确定删除");
JButton button2 = new JButton("取消");
public DeleteUser() {
title.setBounds(150,50,130,50);
title.setFont(new Font("宋体",Font.BOLD,18));
deteleuserid.setBounds(80,100,140,40);
deteleuserpassword.setBounds(80,160,140,40);
inputField1.setBounds(210,100,100,40);
inputField2.setBounds(210,160,100,40);
button1.setBounds(100,210,100,40);
button2.setBounds(220,210,60,40);
setBounds(200,100,500,350);
setResizable(false);
setLayout(null);
setVisible(true);
add(title);
add(deteleuserid);
add(deteleuserpassword);
add(inputField1);
add(inputField2);
add(button1);
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
DeleteShuju dete = new DeleteShuju();
String LoginID = inputField1.getText();
String userpassword = inputField2.getText();
SelectShujuku database=new SelectShujuku();
String userID=database.getDatabySql("select id from user where id="+LoginID,"id"); //从数据库导出登录名
String pswdDatabase=String.valueOf(database.getDatabySql(("select password from user where id="+userID ),"password"));//从数据库导出密码
if(userpassword.equals(pswdDatabase)) {
String sql ="delete from user where id="+LoginID;
dete.deleteUser(sql);
JOptionPane.showMessageDialog(this, "删除成功");
}
else {
JOptionPane.showMessageDialog(this, "密码错误!无法删除");
}
}
if(e.getSource()==button2) {
new DengluActionEvent();
dispose();
}
}
}
管理显示
package com.Gui;
import java.sql.*;
import javax.swing.*;
import com.shujuku.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ContralUser {
public ContralUser(){
JFrame jFrame = new JFrame("用户信息");
jFrame.setSize(500,500);//大小
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);//居中
jFrame.setResizable(false);
//创建面板
JPanel panel = new JPanel();
//表格
Chaxun search = new Chaxun();
Object[][] database1 = search.getDatabySql("select id,name,shenfen,mymoney,password from user");
Object[] col = {"用户名","用户姓名","身份ID","存款"};
JTable table = new JTable(database1, col);//表头和内容放入表
table.setRowHeight(20);//设置行高
table.setPreferredScrollableViewportSize(new Dimension(400,300));//整张表的大小
JScrollPane scrollPane = new JScrollPane(table);//给表整一个滑轮
panel.add(scrollPane);//把表放入面板
//组件
JButton button = new JButton("返回登录");
button.setBounds(200,200,100,100);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
jFrame.dispose();
new DengluActionEvent();
}
}
});
//显示
jFrame.setContentPane(panel);
jFrame.setVisible(true);
}
public static void main(String args[]) {
new ContralUser();
}
}
客服端,服务器
客服端
package com.loginServer;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.Gui.*;
public class LoginClient extends JFrame implements Runnable, ActionListener {
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
JTextField inputText1;
JPasswordField inputText2;
JPanel p1, p2, p3;
JLabel title_1, user_1, password_1;
JButton button1, button2;
JButton button3 = new JButton("注销账户");
JButton button4 = new JButton("管理登录");
JButton connection = new JButton("连接服务器");
public LoginClient() {
socket = new Socket();
inputText1 = new JTextField(10);
inputText2 = new JPasswordField();
title_1 = new JLabel("银行系统");
button1 = new JButton("登录");
button1.setEnabled(false);// 不可见
button2 = new JButton("注册");
button2.setEnabled(false);// 不可见
user_1 = new JLabel("用户名");
password_1 = new JLabel("密码");
inputText2.setEchoChar('*');
user_1.setBounds(60, 100, 60, 35);
password_1.setBounds(60, 150, 60, 35);
inputText1.setBounds(111, 100, 200, 40);
inputText2.setBounds(111, 150, 200, 40);
button1.setBounds(120, 210, 75, 30);
button2.setBounds(210, 210, 75, 30);
button3.setBounds(100, 260, 90, 30);
button4.setBounds(210, 260, 90, 30);
title_1.setBounds(150, 50, 150, 30);
title_1.setFont(new Font("宋体", Font.BOLD, 18));
connection.setBounds(150, 0, 100, 40);
setBounds(100, 100, 460, 360);
button3.setEnabled(false);// 不可见
button4.setEnabled(false);// 不可见
setResizable(true);
setLayout(null);
setVisible(true);
add(title_1);
add(inputText1);
add(inputText2);
add(password_1);
add(user_1);
add(button2);
add(button1);
add(button3);
add(button4);
add(connection);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
connection.addActionListener(this);
inputText1.addActionListener(this);
thread = new Thread(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connection) {
try {
if (socket.isConnected()) {
} else {
InetAddress address = InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress = new InetSocketAddress(address, 2011);
socket.connect(socketAddress);//连接
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
thread.start();
}
} catch (IOException ee) {
socket = new Socket();
}
} else if (e.getSource() == button1) {
String s = inputText1.getText();
if (s != null) {
try {
out.writeUTF("id:" + s);
} catch (IOException e1) {
}
}
}
if (e.getSource() == button2) {
new RegisterActionEvent();
this.setVisible(false);
}
if (e.getSource() == button3) {
new DeleteUser();
dispose();
}
if (e.getSource() == button4) {
if ((inputText1.getText().equals("0")) && (inputText2.getText().equals("0"))) { // 管理登录
new ContralUser();
dispose();
} else {
JOptionPane.showMessageDialog(this, "密码错误!!!");
}
}
}
public void run() {
String s = null;
while (true) {
try {
s = in.readUTF();
String password = new String(inputText2.getPassword());
String userID = inputText1.getText();
if (password.equals(s)) {
new HomepageActionEvent(userID).setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(this, "密码错误!!!");
inputText1.setText("");
inputText2.setText("");
}
} catch (IOException e) {
System.out.println("与服务器已断开");
break;
}
}
}
public static void main(String[] args) {
new LoginClient();
}
}
服务器:
package com.loginServer;
import java.io.*;
import java.net.*;
import java.sql.*;
public class Server {
public static void main(String args[]) {
String url = "jdbc:mysql://localhost:3306/bank?" + "useSSL=true&serverTimezone=UTC";
String user = "root";
String password = "654122";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con;
PreparedStatement sqlOne = null;
ResultSet rs;
try {
con = DriverManager.getConnection(url, user, password);
sqlOne = con.prepareStatement("select * from user where id = ?");//?为占位符,以便后面设置
} catch (SQLException e) {
}
ServerSocket server = null;//服务器
Runnable target;
Thread threadForClient = null;
Socket socketOnServer = null;//客服端接口
while (true) {
try {
server = new ServerSocket(2011);
} catch (IOException e1) {
System.out.println("正在监听");
}
try {
System.out.println("等待客户呼叫");
socketOnServer = server.accept();//接受用户发送的信息
System.out.println("客户的地址:" + socketOnServer.getInetAddress());//得到客服的地址
} catch (IOException e) {
System.out.println("正在等待客户");
}
if (socketOnServer != null) {
target = new Target(socketOnServer, sqlOne);
threadForClient = new Thread(target);
threadForClient.start();//开启线程
}
}
}
}
package com.loginServer;
import java.net.*;
import java.io.*;
import java.sql.*;
public class Target extends Thread implements Runnable {
Socket socket;
DataOutputStream out = null;
DataInputStream in = null;
PreparedStatement sqlOne;
boolean boo = false;
Target(Socket t, PreparedStatement sqlOne) {
socket = t;
this.sqlOne = sqlOne;
try {
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println(e);
}
}
public void run() {
ResultSet rs = null;
while (true) {
try {
String str = in.readUTF();
if (str.startsWith("id:")) {
str = str.substring(str.indexOf(":") + 1);// 用:分隔字符串
sqlOne.setString(1, str);
rs = sqlOne.executeQuery();
}
while (rs.next()) {
boo = true;
String id = rs.getString(1);
String key = rs.getString(3);// 密码
out.writeUTF(key);
}
if (boo == false)
out.writeUTF("无此用户");
} catch (IOException e) {
System.out.println("客户离开" + e);
return;
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
数据库部分
连接数据库
package com.shujuku;
import java.sql.*;
public class ConnectShujuku {//连接数据库
Connection con;
Statement statement;
ResultSet rs;
private String uri="jdbc:mysql://localhost:3306/bank?"+"useSSL=true&serverTimezone=UTC";
private String user="root";
private String password ="654122";
public ConnectShujuku(){}
public void Jiazaicon() {//加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 连接数据库
con = DriverManager.getConnection(uri, user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("连接失败!");
}
}
}
查询所有,存在数组里
package com.shujuku;
import java.sql.*;
public class Chaxun {//查询用户所有的信息
Object[][] database = new String[20][5];
ConnectShujuku data = new ConnectShujuku();//创建数据库连接对象
public Object[][] getDatabySql(String sql){
data.Jiazaicon();//连接数据库
try {
// 建立Statement对象
data.statement = data.con.createStatement();
// Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到数据库。
// 执行数据库查询语句
data.rs = data.statement.executeQuery(sql);
for(int i = 0;data.rs.next();i++) {
database[i][0]= data.rs.getString("id");
database[i][1]= data.rs.getString("name");
database[i][4]= data.rs.getString("password");
database[i][2]= data.rs.getString("shenfen");
database[i][3]= data.rs.getString("mymoney");
}
return database;
}catch (SQLException e) {
e.printStackTrace();
System.out.println("数据读取异常");
}
return null;
}
}
查询单个属性组
package com.shujuku;
import java.sql.*;
public class SelectShujuku {// 查询数据并返回指定列
ConnectShujuku data = new ConnectShujuku();
public String getDatabySql(String sql, String item) {
data.Jiazaicon();
try {
// 建立Statement对象
data.statement = data.con.createStatement();
// Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到数据库。
// 执行数据库查询语句
data.rs = data.statement.executeQuery(sql);
// 语句,该语句返回单个 ResultSet 对象
while (data.rs.next()) {// rs.next()返回布尔值,且光标跳转到下一行
return (data.rs.getString(item).trim());// 此处适应于只需要取一个具体数据的情况,如取具体用户对应的密码。完整代码中还有计算数据长度,取多个数据的方法。
}
if (data.rs != null) {// 读取数据不为空后就关闭data.rs
data.rs.close();
data.rs = null;
}
if (data.statement != null) {// sql语句正确后关闭statement
data.statement.close();
data.statement = null;
}
if (data.con != null) {// 连接成功后,用于关闭连接
data.con.close();
data.con = null;
}
return "";
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据读取异常");
}
return " ";
}
}
更新数据库内容
package com.shujuku;
import java.sql.*;
public class UpdateShujuku {//用于存取钱
ConnectShujuku data = new ConnectShujuku();
public void Updata(String sql) {
data.Jiazaicon();
Statement stmt;
try {
stmt = data.con.createStatement();//创建一个Statement对象
stmt.executeUpdate(sql);//执行sql语句
data.con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
插入数据
package com.shujuku;
import java.sql.*;
public class InsertShujuku {// 插入用户信息
ConnectShujuku data = new ConnectShujuku();
public void Insert(String sql1) {
data.Jiazaicon();
String sql = sql1;
Statement stmt;
try {
stmt = data.con.createStatement();// 创建一个Statement对象
stmt.executeUpdate(sql);// 执行sql语句
System.out.println("插入到数据库成功");
data.con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
删除用户
package com.shujuku;
import java.sql.*;
public class DeleteShuju {//删除用户信息
ConnectShujuku data = new ConnectShujuku();
public void deleteUser(String sql1) {
data.Jiazaicon();//加载驱动并连接数据库
Statement stmt;
try {
stmt = data.con.createStatement();//创建一个Statement对象
stmt.executeUpdate(sql1);//执行sql语句
System.out.println("数据删除成功");
data.con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好像数据库增删改查可以直接用一个函数实现,可以尝试改一下。
数据库的版本是8.0.27,密码是654122
jdk是16.0的。