使用Java GUI实现用户管理系统,连接MYSQL数据库,这是登录界面
下面是用户管理界面,登录之后可以进行相关操作
主要实现过程:启动程序
package manage;
public class StartMysql {
// 启动登录界面
public static void main(String[] args) {
new Login();
}
}
主要界面
package manage;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class MySQLGUI extends JFrame {
JLabel j11=new JLabel("编号 ");
JLabel j12=new JLabel("姓名 ");
JLabel j13=new JLabel("年龄 ");
JLabel j14=new JLabel("地址 ");
JLabel j15=new JLabel("密码 ");
JPanel jp2=new JPanel(); //表头
JLabel j1=new JLabel("编号");
JLabel j2=new JLabel("姓名");
JLabel j3=new JLabel("年龄");
JLabel j4=new JLabel("地址");
JLabel j5=new JLabel("密码");
JTextField f1=new JTextField(10);
JTextField f2=new JTextField(10);
JTextField f3=new JTextField(10);
JTextField f4=new JTextField(15);
JTextField f5=new JTextField(9);
JButton b1=new JButton("查询");
JButton b2=new JButton("修改");
JButton b3=new JButton("删除");
JButton b4=new JButton("清空");
JButton b5=new JButton("添加");
final JTable t1=new JTable();
public MySQLGUI() {
this.setTitle("用户管理");
this.setLayout(null);
this.setLocation(300,100 );
this.setSize(850,600 );
this.setLayout(null);
JPanel panel=new JPanel();
JPanel panel2=new JPanel();
panel.add(t1);
panel.setBounds(50, 130,800, 1000);
jp2.setBounds(55, 100, 800, 100); //表头
this.add(panel);
final Vector<String> title=new Vector<String>();
title.add("编号");
title.add("姓名");
title.add("年龄");
title.add("地址");
title.add("密码");
Vector value=UserManage.query();
final DefaultTableModel model=new DefaultTableModel(value,title);
t1.setModel(model);
if(t1.getRowCount()>0)
{
t1.setRowSelectionInterval(0, 0);
}
t1.addMouseListener(new MouseListener() {
public void mouseReleased1(MouseEvent e) {
}
public void mousePressed1(MouseEvent e) {
// TODO 自动生成的方法存根
}
public void mouseExited1(MouseEvent e) {
// TODO 自动生成的方法存根
}
public void mouseEntered1(MouseEvent e) {
// TODO 自动生成的方法存根
}
public void mouseClicked1(MouseEvent e) {
// TODO 自动生成的方法存根
int row=t1.getSelectedRow();
String id=t1.getValueAt(row, 0).toString();
String name=t1.getValueAt(row, 1).toString();
String age=t1.getValueAt(row, 2).toString();
String address=t1.getValueAt(row, 3).toString();
String pass=t1.getValueAt(row, 4).toString();
f1.setText(id);
f2.setText(name);
f3.setText(age);
f4.setText(address);
f5.setText(pass);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed1(ActionEvent e) {
// TODO 自动生成的方法存根
int id=Integer.valueOf(f1.getText().trim());
String name=f2.getText().trim();
int age=Integer.valueOf(f3.getText().trim());
String address=f4.getText().trim();
String pass=f5.getText().trim();
User good=new User();
good.setId(id);
good.setName(name);
good.setAge(age);
good.setAddress(address);
good.setPass(pass);
UserManage.update(good);
Vector value=UserManage.query();
final DefaultTableModel model=new DefaultTableModel(value,title);
t1.setModel(model);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int sid=Integer.valueOf(f1.getText().trim());
User good=new User();
UserManage.delete(sid);
Vector value=UserManage.query();
final DefaultTableModel model=new DefaultTableModel(value,title);
t1.setModel(model);
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
f1.setText("");
f2.setText("");
f3.setText("");
f4.setText("");
f5.setText("");
}
});
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int id=Integer.valueOf(f1.getText().trim());
String name=f2.getText().trim();
int age=Integer.valueOf(f3.getText().trim());
String address=f4.getText().trim();
String pass=f5.getText().trim();
User good=new User();
good.setId(id);
good.setName(name);
good.setAge(age);
good.setAddress(address);
good.setPass(pass);
UserManage.insert(good);
Vector value=UserManage.query();
final DefaultTableModel model=new DefaultTableModel(value,title);
t1.setModel(model);
}
});
jp2.add(j11); //第一个表头
jp2.add(j12);
jp2.add(j13);
jp2.add(j14);
jp2.add(j15);
this.add(jp2);
panel2.setBounds(0,20,840,100);
panel2.add(j1);
panel2.add(f1);
panel2.add(j2);
panel2.add(f2);
panel2.add(j3);
panel2.add(f3);
panel2.add(j4);
panel2.add(f4);
panel2.add(j5);
panel2.add(f5);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
this.setResizable(false); //设置不可以变大
this.add(panel2);
this.setVisible(true);
}
}
连接数据库操作
package manage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class OperationMySql {
private static OperationMySql dao=new OperationMySql();
public OperationMySql() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据库加载失败"+e.getMessage());
}
}
public static Connection getConn()
{
try {
Connection conn=null;
String url="jdbc:mysql://localhost:3306/UserInfo?&characterEncoding=utf-8&useSSL=false"; //127.0.0.1:3306
conn=DriverManager.getConnection(url,"root","root");
return conn;
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据库连接失败"+e.getMessage());
return null;
}
}
}