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 JSPAn error occurred at line: 8 in the jsp file: /06/137/index.jspGenerated servlet error:
[javac] Compiling 1 source fileD:\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;
^请问这是怎么回事啊?这都是书中的代码,竟然都调不会,不想活了。
<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 JSPAn error occurred at line: 8 in the jsp file: /06/137/index.jspGenerated servlet error:
[javac] Compiling 1 source fileD:\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;
^请问这是怎么回事啊?这都是书中的代码,竟然都调不会,不想活了。
解决方案 »
- 用struts自带的迭代标签提示Cannot find bean: "forumForm" in any scope
- 求教关于session取值的问题!!
- EL和JSTL在开发中用得多吗?
- Help!! JSP如何提示问题
- 能否给一个详细的spring dm的入门例子(不带web功能),能够在console平台上运行的
- struts action 路径问题
- !连接四个表!
- Tomcat抱"BAD packet signature"錯誤,請問是什麼原因?
- 关于JBuilder8中显示中文为乱码,急!在线等待!
- 从数据库里取出的回车符如何判断?
- 看到一些js文件,里面经常会有这样的编码。请大家给解释一下!
- 在ASP.NET很容易的问题,JSP要怎么做?
改成
<jsp:useBean id="connection" scope="request" class="JdbcOdbcConnection"/>