国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

使用Java對數據庫進行基本的查詢和更新操作

2019-11-26 14:56:02
字體:
來源:轉載
供稿:網友

數據庫查詢

利用Connection對象的createStatement方法建立Statement對象,利用Statement對象的executeQuery()方法執行SQL查詢語句進行查詢,返回結果集,再形如getXXX()的方法從結果集中讀取數據。經過這樣的一系列步驟就能實現對數據庫的查詢。

【例】Java應用程序訪問數據庫。應用程序打開考生信息表ksInfo,從中取出考生的各項信息。設考生信息數據庫的結構如下:

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.sql.*;public class Example10_9 extends JFrame implements ActionListener{  public static Connection connectByJdbcodbc(String url, String username,String password){    Connection con = null;    try{      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加載ODBC驅動程序    }    catch (Exception e){      e.printStackTrace();      return null; //加載失敗,連接不成功    }    try{      con = DriverManager.getConnection(url, username, password);    }    catch (SQLException e){      e.printStackTrace();      return null; //連接失敗    }    return con; //連接成功  }  String title[] ={"考號", "姓名", "成績", "地址", "簡歷"};  JTextField txtNo = new JTextField(8);  JTextField txtName = new JTextField(10);  JTextField txtScore = new JTextField(3);  JTextField txtAddr = new JTextField(30);  JTextArea txtresume = new JTextArea();  JButton prev = new JButton("前一個");  JButton next = new JButton("后一個");  JButton first = new JButton("第一個");  JButton last = new JButton("最后一個");  Statement sql; //SQL語句對象  ResultSet rs; //存放查詢結果對象  Example10_9(Connection connect){    super("考生信息查看窗口");    setSize(450, 350);    try{      sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);      rs = sql.executeQuery("SELECT * FROM ksInfo");      Container con = getContentPane();      con.setLayout(new BorderLayout(0, 6); JPanel p[] = new JPanel[4];      for (int i = 0; i < 4; i++){        p[i] = new JPane(new FlowLayout(FlowLayout.LEFT, 8, 0));        p[i].add(new JLabel(title[i]));      }      p[0].add(txtNo);      p[1].add(txtName);      p[2].add(txtScore);      p[3].add(txtAddr);      JPanel p1 = new JPane(new GridLayout94, 1, 0, 8));      JScrollPane jsp = new JScrollPane(txtResume,        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);        jsp.setPreforredSize(new Dimension(300, 60);      for (int i = 0; i < 4; i++){        p1.add(p[i]);      }      JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0);      p2.add(new JLabel(title[4]));      p2.add(jsp);      Jpanel p3 = new Jpanel();      p3.add(prev);      p3.add(next);      p3.add(first);      p3.add(last);      prev.addActionListener(this);      next.addActionListener(this);      first.addActionListener(this);      last.addActionlistener(this);      rs.first();      readRecord();    }    catch (Exception e){      e.printStackTrace():    }    setVisible(ture);  }  public void modifyRecord(Connection connect){    String stuNo = (String)JOptionPane.showInputDialog(null,      "請輸入考生考號", "輸入考號對話框", JOptionPane.PLAIN_MESSAGE, null,      null, "");    try {      sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);      rs = sql.executeQuery("SELECT * FROM ksInfo");      Container con = getContentPane();      con.setLayout(new Boarderlayout(0, 6));      Jpanel p[] = new JPanel[4];      for (int i = 0; i < ; i++){        p[i] = new JPane(new FlowLayout(flowLayout.LEFT, 8, 0));        p[i].add(new JLabel(title[i]));      }      p[0].add(txtNo);      p[1].add(txtName);      p[2].add(txtScore);      p[3].add(txtAddr);      Jpanel p1 = new Jpane(new GridLayout(4, 1, 0, 8));      JScrollPane jsp = new JScrollPane(txtResume,        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);      jsp.setPreferredSize (new dimension(300, 60));      for (int i = 0; i < 4; i++){        p1.add(p[i]);      }      Jpanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));      p2.add(new JLableI(title[4]));      p2.add(jsp);      JPanel p3 = new JPanel();      p3.add(prev);      p3.add(next);      p3.add(first);      p3.add(last);      prev.addActionListener(this);      next.addActionListener(this);      first.addActionListenerIthis);      last.addActionListener(this);      rs.first();      readRecord();    }    catch (Exception e){      e.printStackTrace();    }    setVisible(true);  }  boolean readRecord(){    try{      txtNo.setText(rs.getString("考號")); txtName.setText(rs.getString("姓名")); txtScore.setText(rs.getString("成績"));      txtAddr.setText(rs.getString("地址")); txtResume.setText(rs.getString("簡歷"));    }    catch (SQLException e){      e.printStackTrace(); return false;    }    return true;  }  public void actionPerformed(ActionEvent e){    try{      if (e.getSource() == prev)rs.previous();      else if (e.getSource() == next)rs.next();      else if (e.getSource() == first)rs.first();      else if (e.getSource() == last)rs.last(); readRecord();    }    catch (Exception e2){}  }  public static void main(String args[]){    connection connect = null;    JFrame .setDefaultLookAndFeeDecorated(true);    Font font = new Font("JFrame", Font.PLAIN, 14);    if ((connect =connectByJdbcOdbc("jdbc:odbc:redsun", "xia", "1234")) == null){      JOptionPane.showMessageDialog(null, "數據庫連接失敗!");      System.exit ( - 1);    }    new Example10_9(connect); //創建對象  }}

Java數據庫更新
數據庫更新操作包括數據表創建、刪除、以及數據表記錄的增加、刪除、修改等操作。如果利用數據 SQL命令實現,則利用Statement對旬的executeUpdate()方法,執行SQL的update語句,實現數據表的修改;執行SQL的insert語句,實現數據表記錄的添加。

例如,在前面數據為查詢例子基礎上,再增加對數據表的修改和插入。限于篇幅,不再給出完整程序,只給出實現修改和插入的方法。程序可再增設插入、,,除保存按鈕,通過已有的瀏覽,定位到數據表的特定位置,對癰疽記錄進行編輯修改,或插入,或刪除,然后按保存按鈕,完成修改后的數據表保存。

下面用代碼說明數據表更新的方法。與數據表連接時,需指定獲得的ResultSet 對象是可更新的。

  stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海晏县| 鄯善县| 西乡县| 吴桥县| 禹城市| 苏尼特左旗| 陕西省| 汉中市| 西乌珠穆沁旗| 墨脱县| 赤壁市| 门头沟区| 咸丰县| 平谷区| 如皋市| 宣武区| 南宫市| 明光市| 鄂托克旗| 民权县| 增城市| 榆树市| 舞阳县| 长宁区| 秦皇岛市| 孟连| 简阳市| 廉江市| 霸州市| 高密市| 同仁县| 类乌齐县| 日土县| 慈溪市| 石屏县| 宜君县| 当涂县| 离岛区| 民丰县| 镇赉县| 攀枝花市|