package com.joyo.view;
import com.joyo.entity.Population;
import com.joyo.utils.ArrayUtil;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class PopulationListView {
public static void main(String[] args) {
JTable jTable = new JTable();
Object[] heads = {"序号", "城市", "社区", "街道"};
// 1. 桌子
JFrame jFrame = new JFrame();
jFrame.setTitle("人口普查登记系统");
jFrame.setSize(600, 400);
jFrame.setLocationRelativeTo(null); // 相对屏幕居中
jFrame.setDefaultCloseOperation(3);
// 2. 桌布
JPanel jPanel1 = new JPanel(new FlowLayout());
JPanel jPanel2 = new JPanel(new GridLayout(1, 1));
// 3. 东西
JLabel jLabel = new JLabel("城市");
JTextField jTextField = new JTextField(10);
JButton jButton1 = new JButton("查询");
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入内容
String text = jTextField.getText();
// 连接数据库,查询数据列表
// 1. 导入驱动包
// 2. 加载驱动
ArrayList<Population> dataList = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 3. 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://43.142.24.241:3306/population_db", "root", "launch888.");
// 4. 编写SQl
String sql = "select * from tb_population where city like ?";
// 5. 预编译SQL
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "%" + text + "%");
// 6. 执行SQL,获取结果/集
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// a. 获取数据库字段的值
int id = rs.getInt("id");
String city = rs.getString("city");
String community = rs.getString("community");
String street = rs.getString("street");
// b. 将值存到实体类的属性
Population population = new Population();
population.setId(id);
population.setCity(city);
population.setCommunity(community);
population.setStreet(street);
// c. 将实体类存入集合中
dataList.add(population);
}
String[][] data = ArrayUtil.listToArray(dataList);
// 将数据放入表格中
jTable.setModel(new DefaultTableModel(data, heads));
// 7. 关闭数据库连接
rs.close();
ps.close();
conn.close();
} catch (Exception exe) {
exe.printStackTrace();
System.err.println("出错了");
}
}
});
JButton jButton2 = new JButton("录入");
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 1. 桌子
JDialog jDialog = new JDialog();
jDialog.setSize(300, 200);
jDialog.setTitle("人口普查信息录入");
jDialog.setLocationRelativeTo(null);
// 2. 桌布
JPanel jPanel1 = new JPanel(new FlowLayout());
JPanel jPanel2 = new JPanel(new GridLayout(3, 2));
JPanel jPanel3 = new JPanel(new FlowLayout());
// 3. 东西
JLabel jLabel1 = new JLabel("人口普查信息录入");
JLabel jLabel2 = new JLabel("城市");
JTextField jTextField1 = new JTextField(10);
JLabel jLabel3 = new JLabel("社区");
JTextField jTextField3 = new JTextField(10);
JLabel jLabel4 = new JLabel("街道");
JTextField jTextField4 = new JTextField(10);
JButton jButton1 = new JButton("保存");
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的值
String city = jTextField1.getText();
String community = jTextField3.getText();
String street = jTextField4.getText();
// 判断,如果有内容没有输入,终止执行
if (city.length() == 0) {
JOptionPane.showMessageDialog(null, "城市不能为空");
return;
}
if (community.length() == 0) {
JOptionPane.showMessageDialog(null, "社区不能为空");
return;
}
if (street.length() == 0) {
JOptionPane.showMessageDialog(null, "街道不能为空");
return;
}
// 连接数据库,添加数据
// 连接数据库,查询数据列表
// 1. 导入驱动包
// 2. 加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 3. 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://43.142.24.241:3306/population_db", "root", "launch888.");
// 4. 编写SQl
String sql = "insert into tb_population(id, city, community, street) values (null, ?, ?, ?)";
// 5. 预编译SQL
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, city);
ps.setString(2, community);
ps.setString(3, street);
// 6. 执行SQL,获取结果/集
int rows = ps.executeUpdate();
if (rows >= 1) {
// 提示成功
JOptionPane.showMessageDialog(null, "添加成功");
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
// 7. 关闭数据库连接
ps.close();
conn.close();
} catch (Exception exe) {
exe.printStackTrace();
System.err.println("出错了");
}
}
});
JButton jButton2 = new JButton("取消");
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jDi
没有合适的资源?快使用搜索试试~ 我知道了~
population.zip
共17个文件
class:7个
xml:5个
java:3个
需积分: 0 0 下载量 74 浏览量
2023-06-13
00:32:27
上传
评论
收藏 22KB ZIP 举报
温馨提示
population.zip
资源推荐
资源详情
资源评论
收起资源包目录
population.zip (17个子文件)
population
population
src
com
joyo
utils
ArrayUtil.java 3KB
view
PopulationListView.java 11KB
entity
Population.java 1KB
out
production
population
com
joyo
utils
ArrayUtil.class 4KB
view
PopulationListView$2.class 2KB
PopulationListView$2$2.class 890B
PopulationListView$1.class 4KB
PopulationListView$2$1.class 3KB
PopulationListView.class 4KB
entity
Population.class 1KB
.idea
libraries
mysql_connector_java_8_0_30.xml 307B
workspace.xml 7KB
misc.xml 278B
modules.xml 267B
.gitignore 184B
encodings.xml 200B
population.iml 519B
共 17 条
- 1
资源评论
2301_78617495
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功