利用Connection对象的createStatement方法建立Statement对象,利用Statement对象的executeQuery()方法执行SQL查询语句进行查询,返回结果集,再形如getXXX()的方法从结果集中读取数据。经过这样的一系列步骤就能实现对数据库的查询。
【例 13-9】Java应用程序访问数据库。应用程序打开考生信息表ksInfo,从中取出考生的各项信息。设考生信息数据库的结构如下:
类型 | 字符串 | 字符串 | 整数 | 字符串 | 字符串 |
字段名 | 考号 | 姓名 | 成绩 | 地址 | 简历 |
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知多少(102)多媒体基础