使用Oracle JDBC Driver驱动,连接oracle客户端
1 package DatabaseTest; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.sql.*; 7 8 public class DatabaseConnect extends JFrame implements ActionListener { 9 10 String [] title = {"考号","姓名","成绩","地址","简历" }; 11 JTextField txtNo = new JTextField(30); 12 JTextField txtName = new JTextField(30); 13 JTextField txtScore = new JTextField(30); 14 JTextField txtAddr = new JTextField(30); 15 JTextArea txtResume = new JTextArea(); 16 JButton btPrev = new JButton("前一个"); 17 JButton btNext = new JButton("后一个"); 18 JButton btFirst = new JButton("首个"); 19 JButton btLast = new JButton("末个"); 20 21 Statement sql ; //SQL语句对象 22 ResultSet rs ; // 查询结果对象 23 24 //构造方法 初始化 25 DatabaseConnect(Connection connect){ 26 super("考生信息查看窗口"); 27 setSize(450,300); 28 setLocationRelativeTo(null); //窗体居中 29 try { 30 sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 31 ResultSet.CONCUR_UPDATABLE); //创建SQL语句对象 32 rs = sql.executeQuery("select stuNo,stuName,score,stuAddr,stuResume from ksInfo"); //执行SQL语句 33 Container cont = getContentPane(); 34 cont.setLayout(new FlowLayout()); 35 36 JPanel []p = new JPanel[title.length-1]; 37 for (int i=0; i<title.length-1; i++){ 38 p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT , 8,0)); 39 p[i].add(new JLabel(title[i])); 40 } 41 p[0].add(txtNo); p[1].add(txtName); 42 p[2].add(txtScore); p[3].add(txtAddr); 43 44 JPanel p1 = new JPanel(new GridLayout(4,1,0,8)); //学生基本信息面板 45 for (int i = 0 ; i<title.length-1 ;i++){ 46 p1.add(p[i]); 47 } 48 49 JScrollPane jsp = new JScrollPane(txtResume, 50 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 51 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 52 53 jsp.setPreferredSize(new Dimension(330,60)); 54 55 JPanel p2 = new JPanel(new FlowLayout()); //学生简历面板 56 p2.add(new JLabel(title[title.length-1])); //简历标签名 57 p2.add(jsp); //简历显示文本区 58 59 JPanel p3 = new JPanel(); //按钮面板 60 p3.add(btPrev); p3.add(btNext); p3.add(btFirst); p3.add(btLast); 61 62 cont.add(p1); 63 cont.add(p2); 64 cont.add(p3); 65 66 btPrev.addActionListener(this); 67 btNext.addActionListener(this); 68 btFirst.addActionListener(this); 69 btLast.addActionListener(this); 70 71 rs.first(); 72 readRecord(); 73 74 } catch (SQLException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 System.out.println("列名无效的问题在构造器"); 78 } 79 80 setVisible(true); 81 82 addWindowListener(new WindowAdapter(){ 83 public void closingWindow(WindowEvent e){ 84 System.exit(0); 85 } 86 }); 87 } 88 89 public static Connection connectByJdbcOdbc(String jdbcUrl, String username, String password){ 90 Connection con = null ; 91 try { 92 Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载OracleJDBC驱动 93 } catch (ClassNotFoundException e) { 94 // TODO Auto-generated catch block 95 e.printStackTrace(); 96 System.out.println("加载驱动失败"); 97 return null; //加载驱动失败 98 } 99 100 try { 101 con = DriverManager.getConnection(jdbcUrl, username, password); //建立连接 102 } catch (SQLException e) { 103 // TODO Auto-generated catch block 104 e.printStackTrace(); 105 System.out.println("连接数据库失败"); 106 return null ; //连接数据库失败 107 } 108 109 System.out.println("连接成功"); 110 return con; //连接成功 111 112 } 113 114 public void modifyRecord(Connection connect){ 115 String stuNo = (String)JOptionPane.showInputDialog(null, "请输入考生考号", "输入考号对话框" , JOptionPane.PLAIN_MESSAGE, null, null, ""); 116 117 try { 118 sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 119 ResultSet.CONCUR_READ_ONLY) ; 120 rs = sql.executeQuery("select stuNo,stuName,score,stuAddr,stuResume from ksInfo"); 121 Container cont = getContentPane(); 122 cont.setLayout(new BorderLayout(0,6)); 123 JPanel []p = new JPanel[4]; 124 for (int i=0;i<4;i++){ 125 p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT , 8,0)); 126 p[i].add(new JLabel(title[i])); 127 } 128 p[0].add(txtNo); p[1].add(txtName); 129 p[2].add(txtScore); p[3].add(txtAddr); 130 131 JPanel p1 = new JPanel(new GridLayout(4,1,0,8)); 132 JScrollPane jsp = new JScrollPane(txtResume, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 133 jsp.setPreferredSize(new Dimension(300,60)); 134 135 for (int i = 0 ; i<4 ;i++){ 136 p1.add(p[i]); 137 } 138 139 JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10,0)); 140 141 p2.add(new JLabel(title[4])); p2.add(jsp); 142 143 JPanel p3 = new JPanel(); 144 p3.add(btPrev); p3.add(btNext); p3.add(btFirst); p3.add(btLast); 145 btPrev.addActionListener(this); 146 btNext.addActionListener(this); 147 btFirst.addActionListener(this); 148 btLast.addActionListener(this); 149 150 rs.first(); //游标切换到第一条记录 从头开始读 151 readRecord(); 152 153 } catch (SQLException e) { 154 // TODO Auto-generated catch block 155 e.printStackTrace(); 156 System.out.println("列名无效的问题在modifyRecord"); 157 } 158 159 } 160 161 /* 读取数据 */ 162 boolean readRecord(){ 163 try { 164 txtNo.setText(rs.getString("stuNo")); 165 txtName.setText(rs.getString("stuName")); 166 txtScore.setText(rs.getString("score")); 167 txtAddr.setText(rs.getString("stuAddr")); 168 txtResume.setText(rs.getString("stuResume")); 169 } catch (SQLException e) { 170 // TODO Auto-generated catch block 171 e.printStackTrace(); 172 System.out.println("列名无效的问题在readRecord"); 173 return false; //读取数据失败 174 } 175 return true; //读取数据成功 176 177 } 178 179 @Override 180 public void actionPerformed(ActionEvent arg0) { 181 // TODO Auto-generated method stub 182 try { 183 if (arg0.getSource() == btPrev) { 184 if (!rs.isFirst()) rs.previous(); //切换游标到上一条记录,如果已经是第一条,则不做处理 185 } 186 else if (arg0.getSource() == btNext){ 187 if (!rs.isLast()) rs.next(); //切换游标到下一条记录,如果已经是最后一条,则不做处理 188 } 189 else if (arg0.getSource() == btFirst) rs.first(); //切换游标到第一条记录 190 else if (arg0.getSource() == btLast) rs.last(); //切换游标到最后一条记录 191 readRecord(); // 读取记录 192 } catch (SQLException e) { 193 // TODO Auto-generated catch block 194 e.printStackTrace(); 195 } 196 197 } 198 199 public static void main(String[] args) { 200 // TODO Auto-generated method stub 201 Connection connect = null; 202 JFrame.setDefaultLookAndFeelDecorated(true); 203 Font font = new Font("JFrame", Font.PLAIN, 14) ; 204 String jdbcurl="jdbc:oracle:thin:@10.27.25.241:1521:WHNGBI"; 205 if ((connect = connectByJdbcOdbc(jdbcurl,"BOE_DW","BOE_DW")) == null ){ 206 JOptionPane.showMessageDialog(null, "数据库连接失败!"); 207 System.exit(-1); 208 } 209 210 new DatabaseConnect(connect); 211 } 212 213 }