纯Java增删改查
Posted StephenChowcai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纯Java增删改查相关的知识,希望对你有一定的参考价值。
//自己写的一个完整的带增删改查提交重置功能的表单代码。
package com.l16.test5;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener {
private JPanel panel = null;
private JPanel sePanel = null;
private JPanel hoPanel = null;
private JPanel buPanel = null;
private JPanel buPanel2 = null;
private JPanel buPanel3 = null;
//姓名
private JLabel tilabel = null;
private JLabel naLabel = null;
private JTextField naTextField = null;
//密码
private JLabel paLabel = null;
private JPasswordField passwordField = null;
private JLabel paLabel2 = null;
private JPasswordField passwordField2 = null;
//性别 控制单选用ButtonGroup
private JLabel seLabel = null;
private JRadioButton maRadioButton = null;
private JRadioButton feRadioButton = null;
private ButtonGroup buttonGroup = null;
//爱好
private JLabel hoLabel = null;
private JCheckBox eaCheckBox = null;
private JCheckBox spCheckBox = null;
private JCheckBox slCheckBox = null;
//籍贯 下拉功能用Combobox
private JLabel adLabel = null;
private JComboBox comboBox = null;
//自我介绍
private JLabel shLabel = null;
private JTextArea textArea = null;
private JScrollPane scrollPane = null;
//增删改查 提交 重置
private JButton adButton = null;
private JButton deButton = null;
private JButton alButton = null;
private JButton quButton = null;
private JButton suButton = null;
private JButton reButton = null;
private void init() {
this.setTitle("注册页面");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
Container content = this.getContentPane();
this.panel = new JPanel(new GridBagLayout());
content.add(panel);
GridBagConstraints gbc = new GridBagConstraints();
//标头
this.tilabel = new JLabel("金智用户注册");
this.tilabel.setFont(new Font("宋体", Font.BOLD, 26));
gbc.gridx = 1;
gbc.gridy = 0;
this.panel.add(this.tilabel, gbc);
//位置居右
gbc.anchor = GridBagConstraints.EAST;
//用户名
this.naLabel = new JLabel("用户名:");
this.naLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 1;
this.panel.add(this.naLabel, gbc);
this.naTextField = new JTextField(16);
gbc.gridx = 1;
gbc.gridy = 1;
this.panel.add(this.naTextField, gbc);
//密码1次
this.paLabel = new JLabel("密 码:");
this.paLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 2;
this.panel.add(this.paLabel, gbc);
this.passwordField = new JPasswordField(16);
gbc.gridx = 1;
gbc.gridy = 2;
this.panel.add(this.passwordField, gbc);
//密码2次
this.paLabel2 = new JLabel("密 码2:");
this.paLabel2.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 3;
this.panel.add(this.paLabel2, gbc);
this.passwordField2 = new JPasswordField(16);
gbc.gridx = 1;
gbc.gridy = 3;
this.panel.add(this.passwordField2, gbc);
//性别
this.seLabel = new JLabel("性 别:");
this.seLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 4;
this.panel.add(this.seLabel, gbc);
this.maRadioButton = new JRadioButton("男", true);
this.maRadioButton.setFont(new Font("宋体", Font.BOLD, 20));
this.feRadioButton = new JRadioButton("女");
this.feRadioButton.setFont(new Font("宋体", Font.BOLD, 20));
this.sePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 28, 0));
this.sePanel.add(maRadioButton);
this.sePanel.add(feRadioButton);
this.buttonGroup = new ButtonGroup();
this.buttonGroup.add(maRadioButton);
this.buttonGroup.add(feRadioButton);
gbc.gridx = 1;
gbc.gridy = 4;
this.panel.add(this.sePanel, gbc);
//爱好
this.hoLabel = new JLabel("爱 好:");
this.hoLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 5;
this.panel.add(this.hoLabel, gbc);
this.hoPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.eaCheckBox = new JCheckBox("吃饭");
this.eaCheckBox.setFont(new Font("宋体", Font.BOLD, 16));
this.spCheckBox = new JCheckBox("运动");
this.spCheckBox.setFont(new Font("宋体", Font.BOLD, 16));
this.slCheckBox = new JCheckBox("睡觉");
this.slCheckBox.setFont(new Font("宋体", Font.BOLD, 16));
this.hoPanel.add(eaCheckBox);
this.hoPanel.add(spCheckBox);
this.hoPanel.add(slCheckBox);
gbc.gridx = 1;
gbc.gridy = 5;
this.panel.add(this.hoPanel, gbc);
//籍贯
this.adLabel = new JLabel("籍 贯:");
this.adLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 6;
this.panel.add(this.adLabel, gbc);
String[] add = {"西安", "北京", "上海", "广州"};
this.comboBox = new JComboBox(add);
this.comboBox.setFont(new Font("宋体", Font.BOLD, 20));
this.comboBox.setPreferredSize(new Dimension(176, 22));
gbc.gridx = 1;
gbc.gridy = 6;
this.panel.add(this.comboBox, gbc);
//自我介绍
this.shLabel = new JLabel("介 绍:");
this.shLabel.setFont(new Font("宋体", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 7;
this.panel.add(this.shLabel, gbc);
this.textArea = new JTextArea("请在这里写你的自我介绍", 10, 22);
this.textArea.setFont(new Font("宋体", Font.BOLD, 14));
this.scrollPane = new JScrollPane(textArea);
gbc.gridx = 1;
gbc.gridy = 7;
this.panel.add(this.scrollPane, gbc);
//增删改查
this.buPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
this.adButton = new JButton("增");
this.adButton.addActionListener(this);
this.buPanel.add(this.adButton);
gbc.gridx = 0;
gbc.gridy = 8;
this.panel.add(this.buPanel, gbc);
this.buPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
this.deButton = new JButton("删");
this.deButton.addActionListener(this);
this.alButton = new JButton("改");
this.alButton.addActionListener(this);
this.quButton = new JButton("查");
this.quButton.addActionListener(this);
this.buPanel2.add(this.deButton);
this.buPanel2.add(this.alButton);
this.buPanel2.add(this.quButton);
gbc.gridx = 1;
gbc.gridy = 8;
this.panel.add(this.buPanel2, gbc);
//提交和重置
this.buPanel3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 16 , 0));
this.suButton = new JButton("提交");
this.suButton.addActionListener(this);
this.reButton = new JButton("重置");
this.reButton.addActionListener(this);
this.buPanel3.add(suButton);
this.buPanel3.add(reButton);
gbc.gridx = 1;
gbc.gridy = 9;
this.panel.add(this.buPanel3, gbc);
}
public MyFrame() {
this.init();
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setBounds(100, 20, 400, 600);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//声明数据库的三个对象,
Connection conn = null;//连接对象
Statement sta = null;//执行对象
ResultSet rs = null;//结果集对象
//增加一条记录开始***********************************************
//首先必须查数据库看有没有主键相同的,有的话提醒用户,不能插入同名的两条记录。
if(e.getSource() == this.adButton) {
//获取姓名
String username = this.naTextField.getText();
//校验姓名
if(username != null && username.length() <= 0) {
JOptionPane.showMessageDialog(this, "用户名不能为空!", "提示框", JOptionPane.WARNING_MESSAGE);
return;
}
//获取一次密码
String password1 = String.valueOf(this.passwordField.getPassword());
//获取二次密码
String password2 = String.valueOf(this.passwordField2.getPassword());
//校验密码
if(password1 != null && password1.length() <= 0 ) {
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
if(password2 != null && password2.length() <= 0) {
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
if(!(password1.equals(password2))) {
JOptionPane.showMessageDialog(this, "两次密码不一致,请重新输入二次密码");
return;
}
//获取性别
String sex = null;
if(this.maRadioButton.isSelected()) {
sex = "男";
} else {
sex = "女";
}
//获取爱好
String eat = null;
if(this.eaCheckBox.isSelected()) {
eat = "吃饭";
}
String sport = null;
if(this.spCheckBox.isSelected()) {
sport = "运动";
}
String sleep = null;
if(this.slCheckBox.isSelected()) {
sleep = "睡觉";
}
//获取户籍地
String address = this.comboBox.getSelectedItem().toString();
//获取自我介绍
String showMe = this.textArea.getText();
//拼接增加记录的Sql
String addSql = "insert into userTable values(‘"+username+"‘, ‘"+password1+"‘, ‘"+sex+"‘,"
+ " ‘"+eat+"‘, ‘"+sport+"‘, ‘"+sleep+"‘,"
+ " ‘"+address+"‘, ‘"+showMe+"‘)";
String queSql = "select * from userTable where nameuser = ‘"+username+"‘";
//连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student", "sa", "sa");
sta = conn.createStatement();
rs = sta.executeQuery(queSql);
if(rs.next() == true) {
JOptionPane.showMessageDialog(this, "数据库中已存在此人,不能重复插入");
return;
}
int a = sta.executeUpdate(addSql);
if(a > 0) {
JOptionPane.showMessageDialog(this, "恭喜您,插入记录成功!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(sta != null) {
sta.close();
sta = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//清空表单内的数据
this.naTextField.setText("");
this.passwordField.setText("");
this.passwordField2.setText("");
//默认选择男生
this.maRadioButton.setSelected(true);
this.eaCheckBox.setSelected(false);
this.spCheckBox.setSelected(false);
this.slCheckBox.setSelected(false);
this.comboBox.setSelectedItem("西安");
this.textArea.setText("请在这里写你的自我介绍");
//删除一条记录开始**********************************************
} else if(e.getSource() == this.deButton) {
//根据姓名来删除记录,没有就提示数据库没有
String username = JOptionPane.showInputDialog("请输入您要删除的名字");
//查询数据库
String queSql = "select * from userTable where nameuser = ‘"+username+"‘";
String delSql = "delete from userTable where nameuser = ‘"+username+"‘";
//连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student", "sa", "sa");
sta = conn.createStatement();
rs = sta.executeQuery(queSql);
//校验看数据库是否有此人
if(rs.next() == false) {
JOptionPane.showMessageDialog(this, "查无此人,请您核对后再输入");
} else {
int d = sta.executeUpdate(delSql);
if(d > 0) {
JOptionPane.showMessageDialog(this, "删除记录成功");
}
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(sta != null) {
sta.close();
sta = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//修改一条记录开始***************************************************
} else if(e.getSource() == this.alButton) {
//根据姓名来删除记录,没有就提示数据库没有
String username = JOptionPane.showInputDialog("请输入您要修改的名字");
//查询数据库
String queSql = "select * from userTable where nameuser = ‘"+username+"‘";
//连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student", "sa", "sa");
sta = conn.createStatement();
//可以两次的执行同一个SQL语句
rs = sta.executeQuery(queSql);
//校验,如果数据库里没有此人,则弹框说明
if(rs.next() == false) {
JOptionPane.showMessageDialog(this, "查无此人,请核对后再次输入");
}
//从数据库取出数据放在表单上
rs = sta.executeQuery(queSql);
while(rs.next()) {
this.naTextField.setText(rs.getString("nameuser"));
this.passwordField.setText(rs.getString("userPassword"));
this.passwordField2.setText(rs.getString("userPassword"));
//性别
if(rs.getString("userSex").equals("男")) {
this.maRadioButton.setSelected(true);
} else {
this.feRadioButton.setSelected(true);
}
//爱好
if(rs.getString("userEat").equals("吃饭")) {
this.eaCheckBox.setSelected(true);
}
if(rs.getString("userSport").equals("运动")) {
this.spCheckBox.setSelected(true);
}
if(rs.getString("userSleep").equals("睡觉")) {
this.slCheckBox.setSelected(true);
}
//籍贯
if(rs.getString("userAdress").equals("西安")) {
this.comboBox.setSelectedItem("西安");
} else if(rs.getString("userAdress").equals("北京")) {
this.comboBox.setSelectedItem("北京");
} else if(rs.getString("userAdress").equals("上海")) {
this.comboBox.setSelectedItem("上海");
} else if(rs.getString("userAdress").equals("广州")) {
this.comboBox.setSelectedItem("广州");
}
//自我介绍
this.textArea.setText(rs.getString("userShow"));
JOptionPane.showMessageDialog(this, "每次修改完记录请提交表单");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(sta != null) {
sta.close();
sta = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//查询一条记录开始***************************************************
} else if(e.getSource() == this.quButton) {
//根据姓名来删除记录,
String username = JOptionPane.showInputDialog("请输入您要查询的名字");
//查询数据库
String queSql = "select * from userTable where nameuser = ‘"+username+"‘";
//连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student", "sa", "sa");
sta = conn.createStatement();
rs = sta.executeQuery(queSql);
if(rs.next() == false) {
JOptionPane.showMessageDialog(this, "查无此人,请核对后再次输入");
}
rs = sta.executeQuery(queSql);
//从数据库中查出数据放在页面上
while(rs.next()) {
this.naTextField.setText(rs.getString("nameuser"));
this.passwordField.setText(rs.getString("userPassword"));
this.passwordField2.setText(rs.getString("userPassword"));
//性别
if(rs.getString("userSex").equals("男")) {
this.maRadioButton.setSelected(true);
} else {
this.feRadioButton.setSelected(true);
}
//爱好
if(rs.getString("userEat").equals("吃饭")) {
this.eaCheckBox.setSelected(true);
}
if(rs.getString("userSport").equals("运动")) {
this.spCheckBox.setSelected(true);
}
if(rs.getString("userSleep").equals("睡觉")) {
this.slCheckBox.setSelected(true);
}
//籍贯
if(rs.getString("userAdress").equals("西安")) {
this.comboBox.setSelectedItem("西安");
} else if(rs.getString("userAdress").equals("北京")) {
this.comboBox.setSelectedItem("北京");
} else if(rs.getString("userAdress").equals("上海")) {
this.comboBox.setSelectedItem("上海");
} else if(rs.getString("userAdress").equals("广州")) {
this.comboBox.setSelectedItem("广州");
}
//自我介绍
this.textArea.setText(rs.getString("userShow"));
JOptionPane.showMessageDialog(this, "每次查询完记录请重置表单");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(sta != null) {
sta.close();
sta = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//提交表单记录*******************************************************
//思想先查数据库如果没有主键重复,直接新增一条,如果有先把主键相同的删除,然后把
//修改后的一条记录插入到数据库里
} else if(e.getSource() == this.suButton) {
//获取姓名
String username = this.naTextField.getText();
//校验姓名
if(username != null && username.length() <= 0) {
JOptionPane.showMessageDialog(this, "用户名不能为空!", "提示框", JOptionPane.WARNING_MESSAGE);
return;
}
//获取一次密码
String password1 = String.valueOf(this.passwordField.getPassword());
//获取二次密码
String password2 = String.valueOf(this.passwordField2.getPassword());
//校验密码
if(password1 != null && password1.length() <= 0 ) {
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
if(password2 != null && password2.length() <= 0) {
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
if(!(password1.equals(password2))) {
JOptionPane.showMessageDialog(this, "两次密码不一致,请重新输入二次密码");
return;
}
//获取性别
String sex = null;
if(this.maRadioButton.isSelected()) {
sex = "男";
} else {
sex = "女";
}
//获取爱好
String eat = null;
if(this.eaCheckBox.isSelected()) {
eat = "吃饭";
}
String sport = null;
if(this.spCheckBox.isSelected()) {
sport = "运动";
}
String sleep = null;
if(this.slCheckBox.isSelected()) {
sleep = "睡觉";
}
//获取户籍地
String address = this.comboBox.getSelectedItem().toString();
//获取自我介绍
String showMe = this.textArea.getText();
//拼接增加记录的Sql
String addSql = "insert into userTable values(‘"+username+"‘, ‘"+password1+"‘, ‘"+sex+"‘,"
+ " ‘"+eat+"‘, ‘"+sport+"‘, ‘"+sleep+"‘,"
+ " ‘"+address+"‘, ‘"+showMe+"‘)";
//检验数据库中是否已存在的主键相同的记录,如果有则不能插入
String queSql = "select * from userTable where nameuser = ‘"+username+"‘";
//连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student", "sa", "sa");
sta = conn.createStatement();
//校验
rs = sta.executeQuery(queSql);
if(rs.next() == true) {
JOptionPane.showMessageDialog(this, "数据库中已存在此人,不能重复插入");
return;
}
int a = sta.executeUpdate(addSql);
if(a > 0) {
JOptionPane.showMessageDialog(this, "恭喜您,提交成功!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(sta != null) {
sta.close();
sta = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//清空表单内的数据
this.naTextField.setText("");
this.passwordField.setText("");
this.passwordField2.setText("");
//默认选择男生
this.maRadioButton.setSelected(true);
this.eaCheckBox.setSelected(false);
this.spCheckBox.setSelected(false);
this.slCheckBox.setSelected(false);
this.comboBox.setSelectedItem("西安");
this.textArea.setText("请在这里写你的自我介绍");
//重置表单内容*******************************************************
} else if(e.getSource() == this.reButton) {
//清空表单内的数据
this.naTextField.setText("");
this.passwordField.setText("");
this.passwordField2.setText("");
//默认选择男生
this.maRadioButton.setSelected(true);
this.eaCheckBox.setSelected(false);
this.spCheckBox.setSelected(false);
this.slCheckBox.setSelected(false);
this.comboBox.setSelectedItem("西安");
this.textArea.setText("请在这里写你的自我介绍");
}
}
}
以上是关于纯Java增删改查的主要内容,如果未能解决你的问题,请参考以下文章