读书人

呼救:javabean连接数据库

发布时间: 2013-09-11 18:34:26 作者: rapoo

求救:javabean连接数据库
index.jsp 文件代码:

<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<%@ page import= "java.sql.* " %>
<title> 查询字符串 </title>
</head>
<jsp:useBean id= "connection " scope= "request " class= "com.JDBCconnection "/>

<%request.setCharacterEncoding( "gb2312 ");%>
<body>
<form name= "form1 " method= "post " action= " ">
<p>   </p>
<table width= "711 " height= "169 " border= "1 " align= "center " cellpadding= "0 " cellspacing= "0 " bordercolor= "#6894D5 ">
<tr>
<td width= "122 " height= "32 " bordercolor= "#6894D5 " background= "Images/l_001.jpg " bgcolor= "#FFFFFF " scope= "col "> <div align= "center "> 查询字符串>>> </div> </td>
<td width= "583 " background= "Images/001.jpg " scope= "col "> <div align= "center ">
<p> 查询管理学院的学生信息  <a href= "selectstring.jsp "> 查询 </a> </p>
</div> </td>
</tr>
<tr>
<td height= "135 " background= "Images/l_002.jpg ">   </td>
<td> <p>   </p> <table width= "577 " height= "52 " border= "1 " align= "center " cellpadding= "0 " cellspacing= "0 " bordercolor= "#6894D5 ">
<tr bgcolor= "#A6DFFA ">
<td width= "75 " height= "27 " scope= "col "> <div align= "center "> 学号 </div> </td>
<td width= "63 " scope= "col "> <div align= "center "> 姓名 </div> </td>
<td width= "54 " scope= "col "> <div align= "center "> 年龄 </div> </td>
<td width= "111 " scope= "col "> <div align= "center "> 专业 </div> </td>
<td width= "95 " scope= "col "> <div align= "center "> 学院 </div> </td>


<td width= "137 " scope= "col "> <div align= "center "> 住址 </div> </td>
</tr>
<%
try {
String sql= "select StuId ,StuName ,StuAge ,StuCollege ,StuSpeciality ,StuAddress from tb_stuInfo ";
ResultSet rs=connection.executeQuery(sql);
while (rs.next()){
%>
<tr align= "center ">
<td height= "25 " > <%=rs.getString( "StuID ")%> </td>
<td> <%=rs.getString( "StuName ")%> </td>
<td> <%=rs.getString( "StuAge ")%> </td>
<td> <%=rs.getString( "StuSpeciality ")%> </td>
<td> <%=rs.getString( "StuCollege ")%> </td>
<td> <%=rs.getString( "StuAddress ")%> </td>
</tr>

<%}}catch (Exception ex) {}%>
</table>
<p>   </p> </td>
</tr>
</table>
<p>   </p>
</form>
</body>
</html>

JDBCconnection.java 代码:


package com;
import java.sql.*;
public class JDBCconnection {
private final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver "; //连接sql数据库的方法
private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database06 ";
private final String userName = "sa ";
private final String password = " ";
private Connection con = null;

public JDBCconnection() {
try {
System.out.println(dbDriver);
Class.forName(dbDriver).newInstance(); //加载数据库驱动
} catch (Exception ex) {
ex.printStackTrace();


System.out.println( "数据库加载失败 ");
}
}

//创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);

} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println( "creatConnectionError! ");
}
return true;
}


//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println( "executeQueryError! ");
return null;
}
return rs;
}

//关闭数据库的操作
public void closeConnection() {
if (con != null) {
try {
con.close();


} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println( "Failed to close connection! ");
} finally {
con = null;
}
}
}

}


--------------------------------

出现这样的出错提示:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 8 in the jsp file: /06/137/index.jsp

Generated servlet error:
[javac] Compiling 1 source file

D:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\_06\_137\index_jsp.java:43: package com does not exist
com.JDBCconnection connection = null;
^

请问这是怎么回事啊?这都是书中的代码,竟然都调不会,不想活了。



[解决办法]
<jsp:useBean id= "connection " scope= "request " class= "com.JDBCconnection "/>
改成
<jsp:useBean id= "connection " scope= "request " class= "JdbcOdbcConnection "/>
[解决办法]
你的JDBCconnection没有编译好
[解决办法]
你把JAVABEAN的包去掉吧,然后编译直接放在CLASSES目录下,试试,,很久没接触J2EE了,呵呵学习一下,
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html

读书人网 >Java Web开发

热点推荐