当前位置:Gxlcms > 数据库问题 > Java知多少(108)数据库查询简介

Java知多少(108)数据库查询简介

时间:2021-07-01 10:21:17 帮助过:5人阅读

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知多少(上) Java知多少(中) Java知多少(78)Java向量(Vector)及其应用 Java知多少(79)哈希表及其应用 Java知多少(80)图形界面设计基础 Java知多少(81)框架窗口基础 Java知多少(82)标签、按钮和按钮事件简介 Java知多少(83)面板基础:JPanel和JScrollPane Java知多少(84)图形界面之布局设计 Java知多少(85)文本框和文本区 Java知多少(86)文本框和文本区的输入输出 Java知多少(87)选择框和单选按钮 Java知多少(88)列表和组合框 Java知多少(89)列表和组合框 Java知多少(90)菜单 Java知多少(91)对话框 Java知多少(92)滚动条 Java知多少(93)鼠标事件 Java知多少(94)键盘事件 Java知多少(95)绘图基础 Java知多少(96)绘图之设置字型和颜色 Java知多少(97)绘图模式概述 Java知多少(98)Graphics类的绘图方法 Java知多少(99)Graphics2D类的绘图方法 Java知多少(100)图像处理基础 Java知多少(101)图像缓冲技术 Java知多少(102)多媒体基础 Java知多少(103)网络编程之IP地址和InetAddress类 Java知多少(104)网络编程之统一资源定位符URL Java知多少(105)套接字(Socket) Java知多少(106)程序与数据库连接 Java知多少(107)几个重要的java数据库访问类和接口

Java知多少(108)数据库查询简介

标签:

人气教程排行