我用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">
<tr>
<td width="267" rowspan="2" background="image/lond2.jpg" > </td>
<td width="517" height="195" background="image/lond3.jpg" bgcolor="#69B1F9"> </td>
</tr>
<tr>
<td height="524" align="center" valign="top"><p> </p>
<p class="STYLE1">欢迎登录选课网</p>
<p class="STYLE1"> </p>
<form id="form1" name="form1" method="post" action="lond.jsp">
<label>请选择登录类型
<select name="select" id="select">
<option>学生</option>
<option>老师</option>
<option>管理员</option>
</select>
</label>
<p>
<label>请输入您的用户ID
<input type="text" name="ID" />
</label>
</p>
<p>
请输入您的密码
<label>
<input type="password" name="PASSWORD" />
</label>
</p>
<p> </p>
<p>
<label>
<input type="submit" name="button" id="button" value="提交" />
</label>
<label>
<input type="reset" name="button2" id="button2" value="重置" />
<%if(leixing==6){ %>
<p>您输入的密码有误 请重新输入</p>
<%} %>
</label>
</p>
</form> <p class="STYLE1"> </p></td>
</tr>
</table>
<%if(leixing==3){
response.sendRedirect("gtotal.html");
}%>
<%if(leixing==2){
response.sendRedirect("totalt.html");
}%>
<%if(leixing==1){
response.sendRedirect("total.html");
}%></body>
</html>
在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">
<tr>
<td width="267" rowspan="2" background="image/lond2.jpg" > </td>
<td width="517" height="195" background="image/lond3.jpg" bgcolor="#69B1F9"> </td>
</tr>
<tr>
<td height="524" align="center" valign="top"><p> </p>
<p class="STYLE1">欢迎登录选课网</p>
<p class="STYLE1"> </p>
<form id="form1" name="form1" method="post" action="lond.jsp">
<label>请选择登录类型
<select name="select" id="select">
<option>学生</option>
<option>老师</option>
<option>管理员</option>
</select>
</label>
<p>
<label>请输入您的用户ID
<input type="text" name="ID" />
</label>
</p>
<p>
请输入您的密码
<label>
<input type="password" name="PASSWORD" />
</label>
</p>
<p> </p>
<p>
<label>
<input type="submit" name="button" id="button" value="提交" />
</label>
<label>
<input type="reset" name="button2" id="button2" value="重置" />
<%if(leixing==6){ %>
<p>您输入的密码有误 请重新输入</p>
<%} %>
</label>
</p>
</form> <p class="STYLE1"> </p></td>
</tr>
</table>
<%if(leixing==3){
response.sendRedirect("gtotal.html");
}%>
<%if(leixing==2){
response.sendRedirect("totalt.html");
}%>
<%if(leixing==1){
response.sendRedirect("total.html");
}%></body>
</html>
如果是工程内调试运行运行那么要引入oracle的驱动jar包
这就是异常的图片
我装的有驱动,Oracle中jdbc文件夹下的class12.jar
Javabean已经链接到数据库了,就是jsp中调用javabean的方法出错谢谢大家了!!!!!
<%if(leixing==6){ %>
<p>您输入的密码有误 请重新输入 </p>
<%} %>
不是应该先判断leixing是否==6才输出下面一句吗??
为什么每次运行不管leixing等于几,他都输出。
以前不是这样的啊!
jsp现在有异常是逻辑还是控制台的异常呢?
贴出来……
对了,以前我的那个jsp文件名是小写的,后来不知什么情况,有几个变成大写了,还改不过来了。
其中就有这个jsp页面。
听说404很少是代码错误,是不是和上述情况有关。