struts经常调用的两个类
来源:互联网 发布:耿光刚 大数据 编辑:程序博客网 时间:2024/05/03 10:59
学习开发环境:Myeclipse 7.0 Mysql 6.0 tomcat 6.0
数据库名:school 表名:学籍 列名:学号、姓名、性别、出生年月、家庭地址。
老是用到的两个类:dao.Student类(负责与数据库交互)和po.Student(javabean)
- package dao;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import po.Student;
- //访问数据库
- public class StudentDao {
- private Connection conn = null;
- public void initConnection(){ //初始化数据库连接
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String DbConn = "jdbc:mysql://localhost:3306/school";
- String DbPass = "123";
- conn = java.sql.DriverManager.getConnection(DbConn, "root", DbPass);
- }catch(Exception ex){ex.printStackTrace();}
- }
- public ArrayList queryStus(){ //查询所有的学生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 学号,姓名,性别,出生年月,家庭地址 FROM 学籍 ";
- try{
- this.initConnection();//意味着每次连接都是重新连接数据库,同时在该函数中也会关闭所有的连接。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("学号"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性别"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查询的最后总会关掉连接,以免占用资源
- }
- return stus;
- }
- public ArrayList queryBySex(String sex){ //查询所有的学生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 学号,姓名,性别,出生年月,家庭地址 FROM 学籍 where 性别='"+sex+"'";
- try{
- this.initConnection();//意味着每次连接都是重新连接数据库,同时在该函数中也会关闭所有的连接。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("学号"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性别"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查询的最后总会关掉连接,以免占用资源
- }
- return stus;
- }
- public ArrayList queryBySA(String sex,String add){ //查询所有的学生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 学号,姓名,性别,出生年月,家庭地址 FROM 学籍 where 性别='"+sex+"' and 家庭地址='"+add+"'";
- try{
- this.initConnection();//意味着每次连接都是重新连接数据库,同时在该函数中也会关闭所有的连接。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("学号"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性别"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查询的最后总会关掉连接,以免占用资源
- }
- return stus;
- }
- public void closeConnection(){
- try{
- if(conn!=null){
- conn.close();
- conn = null;
- }
- }catch(Exception ex){ex.printStackTrace();}
- }
- }
- package po;
- //封装一个学生资料
- public class Student {
- private String stuId;
- private String stuName;
- private String stuSex;
- private String stuBir;
- private String stuAdd;
- public String getStuAdd() {
- return stuAdd;
- }
- public void setStuAdd(String stuAdd) {
- this.stuAdd = stuAdd;
- }
- public String getStuBir() {
- return stuBir;
- }
- public void setStuBir(String stuBir) {
- this.stuBir = stuBir;
- }
- public String getStuId() {
- return stuId;
- }
- public void setStuId(String stuId) {
- this.stuId = stuId;
- }
- public String getStuName() {
- return stuName;
- }
- public void setStuName(String stuName) {
- this.stuName = stuName;
- }
- public String getStuSex() {
- return stuSex;
- }
- public void setStuSex(String stuSex) {
- this.stuSex = stuSex;
- }
- }
- struts经常调用的两个类
- STRUTS的两个配置文件----------方便以后调用
- android经常调用的组件
- 经常反复 调用的代码
- Struts中两个相似类的解释
- IE6中经常遇到的两个兼容性问题
- struts的jsonp调用
- Struts的两个配置文件讲解
- Struts的两个配置文件讲解
- Struts中两个相似类的解释DispatchAction
- 使用struts时配置经常出现的问题
- 两个类相互调用的例子
- 两个类相互调用的问题
- 经常使用的两个清爽的table样式
- 经常使用的两个清爽的table样式
- 两个程序经常出现“重试和切换”的解决办法
- 两个二维数组的互换(书上经常碰得到)
- struts的动态方法调用
- 存储过程
- SQL基础
- struts自定义标签学习(三)
- ZOJ1115解题
- 随笔3
- struts经常调用的两个类
- 随便写写
- 陌上花开缓缓归(转)
- How to say future
- 数据库设计中的14个技巧
- 鞍点
- Mysql数据库基础知识
- java 修饰符
- 添加收藏夹功能的js代码