哪位大佬帮我看看为啥,在java里测试好好地,放在jsp里运行就出错,oracle的驱动已经导入了。
哪位大佬帮我看看为啥,在java里测试好好地,放在jsp里运行就出错,oracle的驱动已经导入了。
//加载链接数据库
package po;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class oracle {
static Connection con = null;// 声明Connection对象
public static Connection getConnection() throws Exception
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("数据库驱动加载成功"); // 返回加载驱动成功信息
con = DriverManager.getConnection("jdbc:oracle:"
+ "thin:@127.0.0.1:1521:orcl", "scott", "tiger");
System.out.println("数据库连接成功"); // 返回连接成功信息
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 通过访问数据库的URL获取数据库连接对象
// ,这里后两个参数分别是数据库的用户名及密码
return con;
}
//public static void main(String[] args) throws Exception {
// oracle j=new oracle();
// j.getConnection();
// }
}
//操作取数
package po;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class JdbcTest {
Connection con=null;
Statement sql = null;
ResultSet res = null;
ArrayList<Items> list = new ArrayList<Items>(); // 商品集合
public ArrayList<Items> getAllItems() { // 主方法
//oracle conn =new oracle();
try {
con=oracle.getConnection();
sql = con.createStatement();
String aa = "select * from items";
res = sql.executeQuery(aa);
while (res.next()) {
Items item = new Items();
item.setId(res.getInt("id"));
item.setName(res.getString("name"));
item.setCity(res.getString("city"));
item.setNum(res.getInt("num"));
item.setPrice(res.getInt("price"));
item.setPicture(res.getString("picture"));
list.add(item);// 把一个商品加入集合
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (res != null)
res.close();
if (sql != null)
sql.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// public static void main(String[] args) {
// JdbcTest j=new JdbcTest();
// j.getAllItems();
// ArrayList<Items> list = j.getAllItems();
// if(list!=null&&list.size()>0)
// {
// for(int i=0;i<list.size();i++)
// {
// Items item = list.get(i);
// System.out.println(item.getId()+item.getName());
// }
// }
//
// }
}
//获取数据
package po;
public class Items {
private int id; // 商品编号
private String name; // 商品名称
private String city; // 产地
private int price; // 价格
private int num; // 库存
private String picture; // 商品图片
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
}
//jsp页面
<%@page import="java.net.URLDecoder,java.net.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="po.JdbcTest,po.Items"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
div{
float:left;
margin: 10px;
}
div dd{
margin:0px;
font-size:10pt;
}
div dd.dd_name
{
color:blue;
}
div dd.dd_city
{
color:#000;
}
</style>
</head>
<body>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<!-- 商品循环开始 -->
<%
JdbcTest itemsDao = new JdbcTest();
ArrayList<Items> list = itemsDao.getAllItems();
if(list!=null&&list.size()>0)
{
for(int i=0;i<list.size();i++)
{
Items item = list.get(i);
%>
<div>
<dl>
<dt>
<a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture()%>" width="120" height="90" border="1"/></a>
</dt>
<dd class="dd_name"><%=item.getName() %></dd>
<dd class="dd_city">产地:<%=item.getCity() %> 价格:¥ <%=item.getPrice() %></dd>
</dl>
</div>
<!-- 商品循环结束 -->
<%
}
}
%>
</td>
</tr>
</table>
</center>
</body>
</html>