没看清楚
[此贴子已经被作者于2006-9-15 12:14:59编辑过]
//firm.java
package firm;
import java.sql.*;
import java.io.*;
import java.util.*;
public class firm
{
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
// private static String propFileName = "C:/connDB.properties";
// private static Properties prop = new Properties();
private static String dbClassName ="sun.jdbc.odbc.JdbcOdbcDriver";
private static String dbUrl ="jdbc:odbc:driver=sql server;server=(local);database=GuestBook";
private static String dbUser = "sa";
private static String dbPwd = "";
public static Connection getConnection()
{
Connection conn = null;
try
{
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
}
catch (Exception ee)
{
ee.printStackTrace();
}
if (conn == null)
{
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl + "\r\n用户/密码" + dbUser + "/" +
dbPwd);
}
return conn;
}
/*
*功能:执行查询语句
*/
public ResultSet executeQuery(String sql)
{
try
{
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
/*
*功能:执行更新操作
*/
public int executeUpdate(String sql)
{
int result = 0;
try
{
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
}
catch (SQLException ex)
{
result = 0;
}
try
{
stmt.close();
}
catch (SQLException ex1)
{
}
return result;
}
/*
*功能:关闭数据库的连接
*/
public void close()
{
try
{
if (rs != null)
{
rs.close();
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
try
{
if (stmt != null)
{
stmt.close();
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
try
{
if (conn != null)
{
conn.close();
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
}
////////////////////////////////////////////////////
//index.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="firm" scope="page" class="firm.firm" />
<%
String sql="select * from GuestBook";
ResultSet rs=firm.executeQuery(sql);
String name=null;
if(rs.next()){
name=rs.getString("uname");
}
%>
<html>
<head>
</head>
<body>
<%= name%>
</body>
</html>
//表:
create database GuestBook
create table GuestBook
(
uname varchar(10),
age int
)
insert into GuestBook values('aa',2)
结果为:aa
把驱动和连接改了:
private static String dbClassName ="sun.jdbc.odbc.JdbcOdbcDriver";
private static String dbUrl ="jdbc:odbc:driver=sql server;server=(local);database=GuestBook";
然后你的
<jsp:userBean ...... />
写错了,应该是<jsp:useBean id="firm" scope="page" class="firm.firm" />
韩峰的main方法,我不知道怎么测,所以...........
我用了25楼的,出现这样的!它说50行有错误;
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /11.jsp:6
3: <jsp:useBean id="firm" scope="page" class="firm.firm" />
4: <%
5: String sql="select * from Guest_Book";
6: ResultSet rs=firm.executeQuery(sql);
7: out.print(rs);
8: %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
firm.firm.executeQuery(firm.java:50)
org.apache.jsp._11_jsp._jspService(_11_jsp.java:61)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
[此贴子已经被作者于2006-9-18 12:54:42编辑过]
现在好像好了点,但还是有这么一个错误!难道配置有问题!
An error occurred at line: 3 in the jsp file: /11.jsp
Generated servlet error:
ResultSet cannot be resolved to a type
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
[此贴子已经被作者于2006-9-18 8:26:13编辑过]