jsp文件如下:3.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 此文件的版权归
// Java世纪网(http://www.java2000.net)和 CSDN(www.csdn.net)所有
// 作者:老紫竹
// 转载必须保留此版权声明
//
// 此文采用JDBC链接MySQL数据库,保存计数器信息
%>
<%@page import="net.java2000.count.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello Word! 当前一共访问次数为:<%=CountDB.getInstance().increaseCount()%>
</body>
</html>
javabean文件如下:CountDB.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;/**
* Java世纪网,http://www.java2000.net
*
* @author 赵学庆
*
*/
public class CountDB {
// 数据库的名字
String dbName = "123";
// 登录数据库的用户名
String username = "myname";
// 登录数据库的密码
String password = "mypassword";
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "localhost";
// 数据库的端口,一般不会修改,默认为1433
int port = 3306;
String connectionUrl = "jdbc:mysql://" + host + ":" + port + "/" + dbName+ "?useUnicode=true&characterEncoding=UTF-8"; /**
* 私有的构造器
*/
private CountDB() {
// 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} // 初始化的单例类
private static final CountDB countDB = new CountDB(); /**
* 获得实例的方法
*
* @return 实例
*/
public static CountDB getInstance() {
return countDB;
} /**
* 获得数据库连接
*
* @return 数据库的链接,失败返回null
*/
private Connection getConnection() {
try {
return DriverManager.getConnection(connectionUrl, username, password);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
} /**
* 关闭资源
*
* @param con
* 数据库连接
* @param stmt
* Statement
* @param rs
* 结果集
*/
private void close(Connection con, Statement stmt, ResultSet rs) {
// 关闭我们使用过的资源
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
} /**
* 增加计数的方法。采用了同步机制
*
* @return 当前的计数结果
*/
public synchronized int increaseCount() { Connection con = null; Statement stmt = null; ResultSet rs = null;
try { // 获得一个数据库连接
con = getConnection();
if (con == null) {
return -1;
} String SQL = "SELECT count from t_count";
// 创建查询
stmt = con.createStatement();
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL);
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
} else {
count = 0;
}
// 增加计数
count++;
// 关闭statement
stmt.close();
stmt = con.createStatement();
if (count == 1) {
// 执行增加
return stmt.executeUpdate("insert into t_count (count) values (1)") > 0 ? count
: -1;
} else {
// 执行更新
return stmt.executeUpdate("update t_count set count=count+1") > 0 ? count
: -1;
}
} catch (Exception e) {
// 捕获并显示异常
e.printStackTrace();
return -1;
} finally {
close(con, stmt, rs);
} }
}在Tomcat6下调试,出现错误提示:
An error occurred at line: 19 in the jsp file: /3.jsp
CountDB cannot be resolved
16: <title>Insert title here</title>
17: </head>
18: <body>
19: Hello Word! 当前一共访问次数为:<%=CountDB.getInstance().increaseCount()%>
20: </body>
21: </html>我想可能是编译后的CountDB.class的文件存放位置. 是和3.jsp同一目录,还是放在WEB-INF/classes/下?
密码和用户,以及表结构肯定没错,因为直接jdbc已经通过的.
我的环境是linux+mysql+tomcat6.0.18
pageEncoding="UTF-8"%>
<%
// 此文件的版权归
// Java世纪网(http://www.java2000.net)和 CSDN(www.csdn.net)所有
// 作者:老紫竹
// 转载必须保留此版权声明
//
// 此文采用JDBC链接MySQL数据库,保存计数器信息
%>
<%@page import="net.java2000.count.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello Word! 当前一共访问次数为:<%=CountDB.getInstance().increaseCount()%>
</body>
</html>
javabean文件如下:CountDB.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;/**
* Java世纪网,http://www.java2000.net
*
* @author 赵学庆
*
*/
public class CountDB {
// 数据库的名字
String dbName = "123";
// 登录数据库的用户名
String username = "myname";
// 登录数据库的密码
String password = "mypassword";
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "localhost";
// 数据库的端口,一般不会修改,默认为1433
int port = 3306;
String connectionUrl = "jdbc:mysql://" + host + ":" + port + "/" + dbName+ "?useUnicode=true&characterEncoding=UTF-8"; /**
* 私有的构造器
*/
private CountDB() {
// 注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} // 初始化的单例类
private static final CountDB countDB = new CountDB(); /**
* 获得实例的方法
*
* @return 实例
*/
public static CountDB getInstance() {
return countDB;
} /**
* 获得数据库连接
*
* @return 数据库的链接,失败返回null
*/
private Connection getConnection() {
try {
return DriverManager.getConnection(connectionUrl, username, password);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
} /**
* 关闭资源
*
* @param con
* 数据库连接
* @param stmt
* Statement
* @param rs
* 结果集
*/
private void close(Connection con, Statement stmt, ResultSet rs) {
// 关闭我们使用过的资源
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
} /**
* 增加计数的方法。采用了同步机制
*
* @return 当前的计数结果
*/
public synchronized int increaseCount() { Connection con = null; Statement stmt = null; ResultSet rs = null;
try { // 获得一个数据库连接
con = getConnection();
if (con == null) {
return -1;
} String SQL = "SELECT count from t_count";
// 创建查询
stmt = con.createStatement();
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL);
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
} else {
count = 0;
}
// 增加计数
count++;
// 关闭statement
stmt.close();
stmt = con.createStatement();
if (count == 1) {
// 执行增加
return stmt.executeUpdate("insert into t_count (count) values (1)") > 0 ? count
: -1;
} else {
// 执行更新
return stmt.executeUpdate("update t_count set count=count+1") > 0 ? count
: -1;
}
} catch (Exception e) {
// 捕获并显示异常
e.printStackTrace();
return -1;
} finally {
close(con, stmt, rs);
} }
}在Tomcat6下调试,出现错误提示:
An error occurred at line: 19 in the jsp file: /3.jsp
CountDB cannot be resolved
16: <title>Insert title here</title>
17: </head>
18: <body>
19: Hello Word! 当前一共访问次数为:<%=CountDB.getInstance().increaseCount()%>
20: </body>
21: </html>我想可能是编译后的CountDB.class的文件存放位置. 是和3.jsp同一目录,还是放在WEB-INF/classes/下?
密码和用户,以及表结构肯定没错,因为直接jdbc已经通过的.
我的环境是linux+mysql+tomcat6.0.18
解决方案 »
- request.getSession().getServletContext().getRealPath("/");想取到项目路径,结果报空指针
- 如何用JAVABEAN实现修改操作?
- JSP如何显示连接查询的值?
- 关于数据源的问题,请大家帮下忙
- 用commons-fileupload上传文件的问题,有点难度哦
- 请教,怎么才能用网址直接访问服务器的JSP程序
- 邮箱验证功能求助
- 还是outofmemory的问题,只是相对比较特别,请各们帮帮忙!!!
- 关于一个诡异的正则表达式,求解。求助各位大神
- Mybatis select 一直报The error occurred while setting parameters,jar包为ojdbc14.jar
- javaBean的问题,高手请进
- 一个奇怪的问题,希望牛人能解决。加急!!!
CountDB cannot be resolved 说的很明白了找不到文件
然后在jsp页面头部加上
<jsp:useBean id="CountDB" class="/CountDB" />
在countdb.java放在一个包下,如test,在countdb.java头部加上package test;
CountDB.class放在WEB-INF/classes/下建一个test文件夹,把CountDB.class放进去
然后在jsp页面上加上<jsp:useBean id="CountDB" scope="page" class="test.CountDB" />就可以了