当前位置: 代码迷 >> Java Web开发 >> JSP联接ORACLE没有数据显示
  详细解决方案

JSP联接ORACLE没有数据显示

热度:2178   发布时间:2013-02-25 21:17:36
JSP连接ORACLE没有数据显示
dao.BaseDao
Java code
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();            }        }}


dao.StudentDao

Java code
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;        }}


entity.Student
Java code
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;    }}


jsp页面

Java code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="java.util.List"%><%@page import="dao.StudentDao"%><%@page import="entity.Student"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'Address.jsp' starting page</title>  </head>    <body>    <%        List list = StudentDao.queryAll();        for(int i=0;i<list.size();i++){            Student stu = (Student)list.get(i);            out.print(stu.getStuname());        }     %>  </body></html>
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!