1. 程式人生 > >Swing+JDBC實現增刪查改

Swing+JDBC實現增刪查改

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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class TestFrame extends JFrame implements ActionListener {
 private JLabel ab1, ab2, ab3, ab4;
 private JTextField test1, test2, test3, test4;
 private JButton bu1, bu2, bu3, bu4, bu5, bu6, bu7;
 int cout;
 TestFrame() {
  setTitle("使用者資訊");
  setSize(500, 500);
  setLocation(200, 200);
  setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  setLayout(null);
  ab1 = new JLabel("ID");
  ab1.setBounds(110, 0, 200, 40);
  test1 = new JTextField(15);
  test1.setBounds(160, 0, 100, 30);

  ab2 = new JLabel("姓名");
  ab2.setBounds(110, 50, 200, 40);
  test2 = new JTextField(25);
  test2.setBounds(160, 50, 200, 30);

  ab3 = new JLabel("性別");
  ab3.setBounds(110, 100, 200, 40);
  test3 = new JTextField(25);
  test3.setBounds(160, 100, 200, 30);

  ab4 = new JLabel("年齡");
  ab4.setBounds(110, 150, 200, 40);
  test4 = new JTextField(25);
  test4.setBounds(160, 150, 200, 30);

  bu2 = new JButton("增加");
  bu2.setBounds(300, 250, 100, 30);
  bu2.addActionListener(this);
  bu3 = new JButton("刪除");
  bu3.addActionListener(this);
  bu3.setBounds(100, 250, 100, 30);
  bu4 = new JButton("更新");
  bu4.addActionListener(this);
  bu4.setBounds(100, 340, 100, 30);
  bu1 = new JButton("查詢");
  bu1.setBounds(300, 340, 100, 30);
  bu1.addActionListener(this);

  bu5 = new JButton("上一條");
  bu5.setBounds(100, 295, 100, 30);
  bu5.setVisible(false);
  bu5.addActionListener(this);

  bu6 = new JButton("下一條");
  bu6.setBounds(300, 295, 100, 30);
  bu6.setVisible(false);
  bu6.addActionListener(this);

  bu7 = new JButton("返回");
  bu7.setBounds(200, 295, 100, 30);
  bu7.setVisible(false);
  bu7.addActionListener(this);

  add(ab1);
  add(ab2);
  add(ab3);
  add(ab4);
  add(test1);
  add(test2);
  add(test3);
  add(test4);
  add(bu1);
  add(bu2);
  add(bu3);
  add(bu4);
  add(bu5);
  add(bu6);
  add(bu7);

  setVisible(true);
 }
 public static void main(String[] args) {
  new TestFrame();
 }
 public void actionPerformed(ActionEvent e) {
  SqlTest stt = new SqlTest();
  if (e.getActionCommand().equals("增加")) {
   if("".equals(test1.getText())||"".equals(test2.getText())||"".equals(test3.getText())||"".equals(test4.getText())){
    JOptionPane.showMessageDialog(null, "資料不能為空!!!!");
   }else{
    stt.sqlAdd(new Integer(test1.getText()).intValue(),test2.getText(), test3.getText(), new Integer(test4.getText()).intValue());
   }
  } else if (e.getActionCommand().equals("刪除")) {
   if ("".equals(test1.getText())) {
    JOptionPane.showMessageDialog(null, "ID為空,不能刪除!!!!");
   } else {
    stt.sqlDel(new Integer(test1.getText()).intValue());
   }
  } else if (e.getActionCommand().equals("更新")) {
   if("".equals(test1.getText())||"".equals(test2.getText())||"".equals(test3.getText())||"".equals(test4.getText())){
    JOptionPane.showMessageDialog(null, "資料不能為空!!!!");
   }else{
   stt.sqlUpdata(new Integer(test1.getText()).intValue(), test2.getText(), test3.getText(), new Integer(test4.getText()).intValue());
   }
  } else if (e.getActionCommand().equals("查詢")) {
   Connection conn = SqlTest.sqlConn();
   try {
    Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    ResultSet re = sta.executeQuery("select * from stu");
    re.first();
    cout = re.getRow();
    test1.setText(String.valueOf(re.getInt(1)));
    test2.setText(re.getString(2));
    test3.setText(re.getString(3));
    test4.setText(String.valueOf(re.getInt(4)));
   } catch (SQLException e1) {
    e1.printStackTrace();
   }finally{
    try {
     conn.close();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
   bu1.setVisible(false);
   bu2.setVisible(false);
   bu3.setVisible(false);
   bu4.setVisible(false);
   bu5.setVisible(true);
   bu6.setVisible(true);
   bu7.setVisible(true);
  } else if (e.getActionCommand().equals("返回")) {
   bu1.setVisible(true);
   bu2.setVisible(true);
   bu3.setVisible(true);
   bu4.setVisible(true);
   bu5.setVisible(false);
   bu6.setVisible(false);
   bu7.setVisible(false);
  } else if (e.getActionCommand().equals("上一條")) {
   Connection conn = SqlTest.sqlConn();
   cout--;
   try {
    String sql = "select * from stu";
    Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    ResultSet re = sta.executeQuery(sql);
    re.absolute(cout);
    test1.setText(String.valueOf(re.getInt("id")));
    test2.setText(re.getString("us_name"));
    test3.setText(re.getString("sex"));
    test4.setText(String.valueOf(re.getInt("age")));
   } catch (SQLException e1) {
    JOptionPane.showMessageDialog(this, "沒有上一條記錄了啊!!!!");
    cout++;
   }finally{
    try {
     conn.close();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
  } else if (e.getActionCommand().equals("下一條")) {
   Connection conn = SqlTest.sqlConn();
   cout++;
   try {
    String sql = "select * from stu";
    Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    ResultSet re = sta.executeQuery(sql);
    re.absolute(cout);
    test1.setText(String.valueOf(re.getInt("id")));
    test2.setText(re.getString("us_name"));
    test3.setText(re.getString("sex"));
    test4.setText(String.valueOf(re.getInt("age")));
   } catch (SQLException e1) {
    JOptionPane.showMessageDialog(this, "沒有下一條記錄了啊!!!!");
    cout--;
   }finally{
    try {
     conn.close();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
  }
 }
}
-------------------------------------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class SqlTest {

 public static Connection sqlConn(){
  Connection conn = null;
  try {
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=strTB","sa","");
  } catch (ClassNotFoundException e) {
   JOptionPane.showMessageDialog(null,"!!!!");
  } catch (SQLException e) {
   JOptionPane.showMessageDialog(null,"!!!!");
  }
 
  return conn;
  
 }
 public static void sqlAdd(int test1,String test2,String test3,int test4){
  Connection conn = sqlConn();
  
  String sql = "insert into stu(id,us_name,sex,age) values(?,?,?,?)";
  try {
    String sql1 = "select * from stu where id ="+test1;
    Statement str1 = conn.createStatement();
    ResultSet re = str1.executeQuery(sql1);
    boolean boo = re.next();
    System.out.println(boo);
   if(!boo){
    
    PreparedStatement pst = conn.prepareStatement(sql);
    pst.setInt(1, test1);
    pst.setString(2, test2);
    pst.setString(3,test3);
    pst.setInt(4,test4);
    pst.executeUpdate();
    JOptionPane.showMessageDialog(null, "ID為" + test1+ "資料增加成功");
   }else{
    JOptionPane.showMessageDialog(null, "ID為" + test1+ "ID已存在");
   }
  } catch (SQLException e) {
   JOptionPane.showMessageDialog(null,"有相同的ID!");
  }finally{
   try {
    conn.close();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  } 
 }
 //沒有使用
 public static Person sqlSelect(){
  Connection conn = sqlConn();
  Person p = null;
  String sql = "select * from stu ";
  try {
   Statement sta = conn.createStatement();
   ResultSet re = sta.executeQuery(sql);
   re.first();
   int test1 = re.getInt("id");
   String test2 = re.getString("us_name");
   String test3 = re.getString("sex");
   int test4 = re.getInt("age");
   p = new Person(test1,test2,test3,test4);
  } catch (SQLException e) {
   e.printStackTrace();
  }finally{
   try {
    conn.close();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  }
  return p;
 }
 public static void sqlDel(int test1){
  Connection conn = sqlConn();
  String sql = "delete from stu where id="+test1;
  try {
   Statement sta = conn.createStatement();
   sta.executeUpdate(sql);
   JOptionPane.showMessageDialog(null, "刪除成功!!!!");
  } catch (SQLException e) {
   JOptionPane.showMessageDialog(null,"沒有找到ID!");
  }finally{
   try {
    conn.close();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  }
 }
 public void sqlUpdata(int test1,String test2,String test3,int test4){
  Connection conn = sqlConn();
  try {
   String sql1 = "select * from stu where id ="+test1;
   Statement str1 = conn.createStatement();
   ResultSet re = str1.executeQuery(sql1);
   boolean boo = re.next();
   if(boo){
    String sql = "update stu set  us_name = '"+test2+"',sex ='"+test3+"',age = "+test4+" where id="+test1;
    Statement str = conn.createStatement();
    int se = str.executeUpdate(sql);
    JOptionPane.showMessageDialog(null, "更新成功");
    System.out.println(se);
   }else{
    JOptionPane.showMessageDialog(null, "沒有這條記錄!!!");
   }
  } catch (SQLException e1) {
   JOptionPane.showMessageDialog(null, "輸入有問題!"); ;
  }finally{
   try {
    conn.close();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  }
 }
}

轉:http://blog.sina.com.cn/s/blog_5f06ab450100csag.html