利用Connection對象的createStatement方法創建Statement對象,利用Statement對象的executeQuery()方法執行SQL查詢語句進行查詢,返回結果集,再形如getXXX()的方法從結果集中讀取數據。通過這樣的一系列步驟就能實現對數據庫的查詢。
【例 13-9】Java應用程序訪問數據庫。應用程序打開考生信息表ksInfo,從中取出考生的各項信息。設考生信息數據庫的結構以下:html
類型 | 字符串 | 字符串 | 整數 | 字符串 | 字符串 |
字段名 | 考號 | 姓名 | 成績 | 地址 | 簡歷 |
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import java.util.*; 5 import java.sql.*; 6 public class Example10_9 extends JFrame implements ActionListener{ 7 public static Connection connectByJdbcodbc(String url, String username,String password){ 8 Connection con = null; 9 try{ 10 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加載ODBC驅動程序 11 } 12 catch (Exception e){ 13 e.printStackTrace(); 14 return null; //加載失敗,鏈接不成功 15 } 16 try{ 17 con = DriverManager.getConnection(url, username, password); 18 } 19 catch (SQLException e){ 20 e.printStackTrace(); 21 return null; //鏈接失敗 22 } 23 return con; //鏈接成功 24 } 25 String title[] ={"考號", "姓名", "成績", "地址", "簡歷"}; 26 JTextField txtNo = new JTextField(8); 27 JTextField txtName = new JTextField(10); 28 JTextField txtScore = new JTextField(3); 29 JTextField txtAddr = new JTextField(30); 30 JTextArea txtresume = new JTextArea(); 31 JButton prev = new JButton("前一個"); 32 JButton next = new JButton("後一個"); 33 JButton first = new JButton("第一個"); 34 JButton last = new JButton("最後一個"); 35 Statement sql; //SQL語句對象 36 ResultSet rs; //存放查詢結果對象 37 Example10_9(Connection connect){ 38 super("考生信息查看窗口"); 39 setSize(450, 350); 40 try{ 41 sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 42 rs = sql.executeQuery("SELECT * FROM ksInfo"); 43 Container con = getContentPane(); 44 con.setLayout(new BorderLayout(0, 6); JPanel p[] = new JPanel[4]; 45 for (int i = 0; i < 4; i++){ 46 p[i] = new JPane(new FlowLayout(FlowLayout.LEFT, 8, 0)); 47 p[i].add(new JLabel(title[i])); 48 } 49 p[0].add(txtNo); 50 p[1].add(txtName); 51 p[2].add(txtScore); 52 p[3].add(txtAddr); 53 JPanel p1 = new JPane(new GridLayout94, 1, 0, 8)); 54 JScrollPane jsp = new JScrollPane(txtResume, 55 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 56 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 57 jsp.setPreforredSize(new Dimension(300, 60); 58 for (int i = 0; i < 4; i++){ 59 p1.add(p[i]); 60 } 61 JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0); 62 p2.add(new JLabel(title[4])); 63 p2.add(jsp); 64 Jpanel p3 = new Jpanel(); 65 p3.add(prev); 66 p3.add(next); 67 p3.add(first); 68 p3.add(last); 69 prev.addActionListener(this); 70 next.addActionListener(this); 71 first.addActionListener(this); 72 last.addActionlistener(this); 73 rs.first(); 74 readRecord(); 75 } 76 catch (Exception e){ 77 e.printStackTrace(): 78 } 79 setVisible(ture); 80 } 81 public void modifyRecord(Connection connect){ 82 String stuNo = (String)JOptionPane.showInputDialog(null, 83 "請輸入考生考號", "輸入考號對話框", JOptionPane.PLAIN_MESSAGE, null, 84 null, ""); 85 try { 86 sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 87 rs = sql.executeQuery("SELECT * FROM ksInfo"); 88 Container con = getContentPane(); 89 con.setLayout(new Boarderlayout(0, 6)); 90 Jpanel p[] = new JPanel[4]; 91 for (int i = 0; i < ; i++){ 92 p[i] = new JPane(new FlowLayout(flowLayout.LEFT, 8, 0)); 93 p[i].add(new JLabel(title[i])); 94 } 95 p[0].add(txtNo); 96 p[1].add(txtName); 97 p[2].add(txtScore); 98 p[3].add(txtAddr); 99 Jpanel p1 = new Jpane(new GridLayout(4, 1, 0, 8)); 100 JScrollPane jsp = new JScrollPane(txtResume, 101 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 102 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 103 jsp.setPreferredSize (new dimension(300, 60)); 104 for (int i = 0; i < 4; i++){ 105 p1.add(p[i]); 106 } 107 Jpanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0)); 108 p2.add(new JLableI(title[4])); 109 p2.add(jsp); 110 JPanel p3 = new JPanel(); 111 p3.add(prev); 112 p3.add(next); 113 p3.add(first); 114 p3.add(last); 115 prev.addActionListener(this); 116 next.addActionListener(this); 117 first.addActionListenerIthis); 118 last.addActionListener(this); 119 rs.first(); 120 readRecord(); 121 } 122 catch (Exception e){ 123 e.printStackTrace(); 124 } 125 setVisible(true); 126 } 127 boolean readRecord(){ 128 try{ 129 txtNo.setText(rs.getString("考號")); txtName.setText(rs.getString("姓名")); txtScore.setText(rs.getString("成績")); 130 txtAddr.setText(rs.getString("地址")); txtResume.setText(rs.getString("簡歷")); 131 } 132 catch (SQLException e){ 133 e.printStackTrace(); return false; 134 } 135 return true; 136 } 137 public void actionPerformed(ActionEvent e){ 138 try{ 139 if (e.getSource() == prev)rs.previous(); 140 else if (e.getSource() == next)rs.next(); 141 else if (e.getSource() == first)rs.first(); 142 else if (e.getSource() == last)rs.last(); readRecord(); 143 } 144 catch (Exception e2){} 145 } 146 public static void main(String args[]){ 147 connection connect = null; 148 JFrame .setDefaultLookAndFeeDecorated(true); 149 Font font = new Font("JFrame", Font.PLAIN, 14); 150 if ((connect =connectByJdbcOdbc("jdbc:odbc:redsun", "xia", "1234")) == null){ 151 JOptionPane.showMessageDialog(null, "數據庫鏈接失敗!"); 152 System.exit ( - 1); 153 } 154 new Example10_9(connect); //建立對象 155 } 156 }
系列文章:java