资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
任务二十三 访问数据升级3P239程序代码package com.task23;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;/* * StudentDAO.java * 数据库操作DAO,查询 */public class StudentDAO public List getAllStudent() /详细代码参见23.4.4 数据访问层package com.task23;/* * Student.java * 封装实体Bean */public class Student private String id ;private String name ;private int age ;private String speci ;public Student() super();public Student(String id, String name, int age, String speci) super();this.id = id;this.name = name;this.age = age;this.speci = speci;public String getId() return id;public void setId(String id) this.id = id;public String getName() return name;public void setName(String name) this.name = name;public int getAge() return age;public void setAge(int age) this.age = age;public String getSpeci() return speci;public void setSpeci(String speci) this.speci = speci;package com.task23;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/* * StudentDBConnection.java * 辅助类,数据库连接,资源关闭 */public class StudentDBConnection private static final String DRIVER_CLASS = com.microsoft.jdbc.sqlserver.SQLServerDriver;private static final String DATABASE_URL = jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=welcomestudent;private static final String DATABASE_USER = sa;private static final String DATABASE_PASSWORD = sasa;/* * 建立连接 * * return Connection */public static Connection getConnection() Connection con =null;try Class.forName(DRIVER_CLASS); catch (ClassNotFoundException ce) ce.printStackTrace();try con = DriverManager.getConnection(DATABASE_URL, DATABASE_USER,DATABASE_PASSWORD); catch (SQLException e) e.printStackTrace();return con;/* * 关闭连接 */public static void closeConnection(Connection con)tryif (con!=null & !con.isClosed()con.close();catch(Exception e)e.printStackTrace();/* * 关闭结果集 */public static void closeResultSet(ResultSet rs) try if (rs != null) rs.close(); catch (SQLException e) e.printStackTrace();/* * 关闭Statement */public static void closeStatement(Statement pstmt) try if (pstmt != null) pstmt.close(); catch (SQLException e) e.printStackTrace();package com.task23;import java.util.ArrayList;import java.util.List;/* * StudentBiz.java * 业务逻辑 */public class StudentBiz public static void show(Student student) if (student != null) System.out.print(student.getId() + t);System.out.print(student.getName() +t);System.out.print(student.getAge() + t);System.out.println(student.getSpeci();/* * param args */public static void main(String args) StudentDAO studentDAO = new StudentDAO();Student student = null;/ 查listList lStudent = new ArrayList();lStudent = studentDAO.getAllStudent();for (int i = 0; i lStudent.size(); i+) student = (Student) lStudent.get(i);show(student);P243应用扩展package com.task23;import java.util.List;/* * StudentDAOInterface.java * 数据库操作DAO接口,查、增、删、改 */public interface StudentDAOInterface public Student getStudentById(String id) ;public List getAllStudent();public int addStudent(Student student);public int deleteStudent(Student student);public int updateStudent(Student student) ;package com.task23;/* * StudentBizInterface.java * 业务逻辑接口 */public interface StudentBizInterface public void show(Student student);public void business(); public class StudentDAO implements StudentDAOInterface/实现接口代码参见23.4.4 数据访问层public class StudentBiz implements StudentBizInterfacepublic void show(Student student) public void business()
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号