日期:2014-05-17 浏览次数:20732 次
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BaseDao { public static Connection conn(){ Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(ResultSet rs,Statement stm,Connection conn){ try { if(rs!=null){ rs.close(); } if(stm!=null){ stm.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import entity.Student; public class StudentDao { public static List<Student> queryAll(){ Connection conn = null; PreparedStatement pstm = null; ResultSet rs = null; List list = new ArrayList(); try{ conn = BaseDao.conn(); String sql = "select * from studentinfo"; pstm = conn.prepareStatement(sql); rs=pstm.executeQuery(); while(rs.next()){ Student stu = new Student(); stu.setStuno(rs.getString(1)); stu.setStuname(rs.getString(2)); stu.setSex(rs.getString(3)); stu.setStuage(rs.getInt(4)); list.add(stu); } }catch(Exception e){ e.printStackTrace(); }finally{ BaseDao.close(rs, pstm, conn); } return list; } }
package entity; public class Student { private String stuno; private String stuname; private String sex; private int stuage; public String getStuno() { return stuno; } public void setStuno(String stuno) { this.stuno = stuno; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getStuage() { return stuage; } public void setStuage(int stuage) { this.stuage = stuage; } }