我用jsp和javabean做了一个学生选课系统。javabean中的tolond方法连接Oracle数据库。tolond方法有int行的返回值,jsp根据返回值的不通跳转的不同的页面。
在javabean中测试,连接数据库成功,能取出数据达到预期的效果。
但是在jsp中调用tolond方法就会出现加载驱动异常,sql异常。
以前是连接MySQL数据库的,并且成功了,改成Oracle后,jsp页面就不能正常运行了。
耽误大家一点时间,请大家帮帮忙。谢谢了!!!!!!
代码如下:
javabean:
package wangzhan;
import java.sql.*;
public class lond {
private String first;
private int ID;
private int PASSWORD;
public void setFirst(String first){
this.first=first;
}
public void setID(int ID){
this.ID=ID;
}
public void setPASSWORD(int PASSWORD){
this.PASSWORD=PASSWORD;
}
public int getID(){
return this.ID;
}
public int getPASSWORD(){
return this.PASSWORD;
}
public int tolond(){
String url = "jdbc:oracle:thin:@localhost:1521:orcl1";
String userName = "system";
String password = "o123";
Connection conn = null;
String password1=null;
String pop=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
conn = DriverManager.getConnection(url, userName, password);
//创建PreparedStatement语句
PreparedStatement pstmtSelect = conn.prepareStatement(
"SELECT password,pop FROM student WHERE stu_id=?");
int id=ID;
pstmtSelect.setString(1, Integer.toString(id));
//执行PreparedStatement语句
ResultSet rs = pstmtSelect.executeQuery();
if(!rs.next())
return 4;
do{
password1=rs.getString(1);
pop=rs.getString(2);
}while(rs.next());
int pop1=Integer.valueOf(pop);
int password2=Integer.valueOf(password1);
if(PASSWORD==password2){
if(pop1==1)
return 1 ;
if(pop1==2)
return 2;
if(pop1==3)
return 3 ;
}
else
return 6;
pstmtSelect.close();
} catch(SQLException e) {
System.out.println("出现SQLException异常");
} finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}
return 5;
}
public static void main(String args[]){
lond b=new lond();
int q=0;
q=b.tolond();
System.out.println(q);
System.out.println(b.getID());
}
}
jsp代码:
<%@ page contentType="text/html; charset=gb2312"%>
<jsp:useBean id="prince" scope="request" class="wangzhan.lond">
<jsp:setProperty name="prince" property="*"/>
</jsp:useBean>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 60px
}
body {
background-color: #FFFFFF;
}
-->
</style>
<%
String stu_id="stu_id";
int a=prince.getID();
String str=String.valueOf(a);
session.setAttribute(stu_id,str); %>
<body>
<%!
int leixing=0;
%>
<%
try
{
leixing=prince.tolond();
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
<table width="800" border="0" align="center" bgcolor="#69B1F9">