//自己寫的一個完整的帶增刪改查提交重置功能的表單代碼。
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("請在這里寫你的自我介紹");
}
}
}
