package com.common; import java.sql.*; public class DataBaseConn { public Connection conn = null; public void createConn(){ String url = "jdbc:mysql://localhost:3306/employeeDb"; String username = "root";// 数据库用户名 String password = "root";// 数据库密码 if(conn == null){ try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); }catch(Exception e){e.toString();} } } public Connection getConn(){ if(conn == null){ createConn();} return conn; } public void closeConn(){ if(conn != null){ try { conn.close(); conn = null; } catch (Exception e) { e.printStackTrace(); } } } public ResultSet executeQuery(String sql) { ResultSet rs = null; if (conn == null) {createConn();} try { Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch (Exception e) { e.printStackTrace(); } return rs; } public void executeUpdate(String sql) { if (conn == null) {createConn(); } try { Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } } }
<jsp:useBean id="con" class="com.common.DataBaseConn" scope="page"/> <% con.getConn(); String sql="select * from employee_info"; ResultSet rs= con.executeQuery(sql); %> <table border='1' width='100%'> <tr> <th>雇员号</th> <th>姓名</th> <th>出生日期</th><th >薪水</th></tr> <% while( rs.next() ) { out.print("<tr>"); out.print("<td >"+rs.getString(1)+"</td>"); out.print("<td >"+rs.getString(2)+"</td>"); out.print("<td >"+rs.getDate("birthday")+"</td>"); out.print("<td >"+rs.getInt("salary")+"</td>"); out.print("</tr>") ; } out.print("</table>"); con.closeConn(); %>
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/4265.html