81,092
社区成员
发帖
与我相关
我的任务
分享
<%!
String getWishes() throws SQLException{
JSONArray array = new JSONArray();
String sql = "select id,username,wish,wishtime,colorsuit from wishes";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
JSONObject obj = new JSONObject();
obj.put("id",rs.getString(1));
obj.put("username",rs.getString(2));
obj.put("wish", rs.getString(3));
obj.put("wishtime", rs.getString(4));
obj.put("colorsuit", new Integer(rs.getInt(5)));
array.add(obj);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
rs.close();
pstmt.close();
conn.close();
}
return array.toString();
}
void addWish(String username,String wish,String color) throws SQLException{
String sql = "insert into wishes(username,wish,wishtime,colorsuit) values(?,?,now(),?)";
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,wish);
pstmt.setString(3,color);
pstmt.executeUpdate();
}catch(Exception e){System.out.println(e.toString());
}finally{
pstmt.close();
conn.close();
}
}
String getDBTime() throws SQLException{
String dbTime = "";
String sql = "select now()";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
dbTime = rs.getString(1);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
rs.close();
pstmt.close();
conn.close();
}
return dbTime;
}
%>
<%
out.clear();
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
if("getWishes".equals(action)){
out.println(getWishes());
}else if("sendWishes".equals(action)){
String username = request.getParameter("username");
String wish = request.getParameter("wish");
String color = request.getParameter("color");
addWish(username,wish,color);
out.print(getDBTime());
}
%>
<script type="text/javascript">
var maxzIndex = 999999; //最大Z坐标
var currzIndex = 1; //当前Z坐标
var xMin = 10; //X坐标最小值
var xMax = 300; //X坐标最大值
var yMin = 100; //Y坐标最小值
var yMax = 300; //Y坐标最大值
var msMin = 1000; //显示毫秒数最小值
var msMax = 3000; //显示毫秒数最大值
//色彩数组
var colorArray = ["#FF9999","#FF6633","#FFCC00","#99CC66","#00CCCC","#0099CC","#CC99FF"];
//获取随机显示毫秒
function getRanMS() {
return getRanNum(msMin, msMax);
}
//获取随机横坐标
function getRanX() {
return getRanNum(xMin, xMax);
}
//获取随机纵坐标
function getRanY() {
return getRanNum(yMin, yMax);
}
//通用获取随机数的函数
function getRanNum(minVal, maxVal) {
return Math.round(Math.random() * (maxVal - minVal)) + minVal;
}
//页面加载完毕自动初始化函数
$(function() {
//为选择颜色列表项赋值背景色
$("option").each(
function(i) {
$(this).background(colorArray[i]);
}
);
//向服务器发送请求获取许愿信息
$.getJSON("wishes.jsp", //服务器页面地址
{
action: "getWishes" //action参数
},
function(json) {
//遍历结果数组
$(json).each(
function(i) {
buildWish(json[i]); //建立愿望节点
}
);
}
);
});
//点击“关闭”后隐藏愿望
function hideWish(src) {
$(src).parent().slideUp();
}
//发送新愿望给服务器
function sendWish() {
var username = $("#username").val(); //获取发送用户姓名
var wish = $("#wish").val(); //获取愿望内容
var color = $("#color").val(); //获取颜色
if (username != "" && wish != "") {
document.forms[0].reset(); //表单重置
$.post("wishes.jsp", //服务器页面地址
{
action: "sendWish", //action参数
username: username, //许愿人参数
wish: wish, //愿望参数
color: color //颜色参数
},
function(time) {
//将新愿望各参数组成json对象
var json = {
username: username,
wishtime: time,
wish: wish,
colorsuit:color
};
buildWish(json); //建立愿望节点
}
);
} else {
alert("许愿人和愿望都需要填写内容。");
}
}
//在页面建立愿望
function buildWish(json) {
var x = getRanX(); //随机生成X坐标
var y = getRanY(); //随机生成Y坐标
var ms = getRanMS(); //随机生成显示时间
//创建愿望节点
var divNode = $(document.createElement("div"));
divNode
//添加样式
.addClass("wish")
//追加许愿时间
.append("<div class='wishtime'>" +
json.wishtime.substr(0,16) +
"</div>")
//追加愿望信息
.append(json.wish)
//追加许愿人
.append("<div class='username'>" +
json.username +
"</div>")
//追加关闭功能
.append("<div class='close' onclick='hideWish(this)'>关闭</div>")
//将节点写入body
.appendTo("body")
//设定鼠标点击事件
.mousedown(
function() {
this.style.zIndex = currzIndex++; //Z坐标增加1
})
//设置可拖拽
.Draggable(
{
zIndex:maxzIndex //拖拽过程中的Z坐标值
})
//设置left位置
.left(x+"px")
//设置top位置
.top(y+"px")
//设置背景颜色
.background(colorArray[json.colorsuit-1])
//使用随机生成的毫秒数淡入
.fadeIn(ms);
}
</script>
链接类型:null
链接位置:null
用户/密码null/null
java.lang.NullPointerException
空指针异常,数据库连接类有问题,连接的配置没有配,至少用户名密码没配<%!
String getWishes() throws SQLException{
JSONArray array = new JSONArray();
String sql = "select id,username,wish,wishtime,colorsuit from wishes";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
JSONObject obj = new JSONObject();
obj.put("id",rs.getString(1));
obj.put("username",rs.getString(2));
obj.put("wish", rs.getString(3));
obj.put("wishtime", rs.getString(4));
obj.put("colorsuit", new Integer(rs.getInt(5)));
array.add(obj);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
rs.close();
pstmt.close();
conn.close();
}
return array.toString();
}
void addWish(String username,String wish,String color) throws SQLException{
String sql = "insert into wishes(username,wish,wishtime,colorsuit) values(?,?,now(),?)";
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,wish);
pstmt.setString(3,color);
pstmt.executeUpdate();
}catch(Exception e){System.out.println(e.toString());
}finally{
pstmt.close();
conn.close();
}
}
String getDBTime() throws SQLException{
String dbTime = "";
String sql = "select now()";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
dbTime = rs.getString(1);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
rs.close();
pstmt.close();
conn.close();
}
return dbTime;
}
%>
链接类型:null
链接位置:null
用户/密码null/null
java.lang.NullPointerException
2012-12-24 12:06:30 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.wishes_jsp.getWishes(wishes_jsp.java:39)
at org.apache.jsp.wishes_jsp._jspService(wishes_jsp.java:150)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
package com.dao;
//将该类保存到com.tools包中
import java.io.InputStream; //导入java.io.InputStream类
import java.sql.*; //导入java.sql包中的所有类
import java.util.Iterator;
import java.util.Map;
import java.util.Properties; //导入java.util.Properties类
import java.util.Map.Entry;
public class ConnDB {
public Connection conn = null; // 声明Connection对象的实例
public Statement stmt = null; // 声明Statement对象的实例
public ResultSet rs = null; // 声明ResultSet对象的实例
public PreparedStatement prpdStmt = null;// 声明PreparedStatement对象的实例
private static String propFileName = "/com/dao/connDB.properties"; // 指定资源文件保存的位置
private static Properties prop = new Properties(); // 创建并实例化Properties对象的实例
private static String dbClassName;// 定义保存数据库驱动的变量
private static String dbUrl;
private static String dbUser;
private static String dbPwd;
public ConnDB() { // 定义构造方法
try { // 捕捉异常
// 将Properties文件读取到InputStream对象中
InputStream in = getClass().getResourceAsStream(propFileName);
prop.load(in); // 通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动
dbUrl = prop.getProperty("DB_URL", dbUrl); // 获取URL
dbUser = prop.getProperty("DB_USER", dbUser); // 获取登录用户
dbPwd = prop.getProperty("DB_PWD", dbPwd); // 获取密码
} catch (Exception e) {
e.printStackTrace(); // 输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try { // 连接数据库时可能发生异常因此需要捕捉该异常
Class.forName(dbClassName).newInstance(); // 装载数据库驱动
// 建立与数据库URL中定义的数据库的连接
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(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql); // 执行SQL语句,并返回一个ResultSet对象rs
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
return rs; // 返回结果集对象
}
/*
* 功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0; // 定义保存返回值的变量
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); // 执行更新操作
} catch (SQLException ex) {
result = 0; // 将保存返回值的变量赋值为0
}
return result; // 返回保存返回值的变量
}
// 功能: 对数据库的增加、修改和删除的操作,布尔型
public boolean executeUpdater(String sql) {
int result = 0; // 定义保存返回值的变量
try {// 捕捉异常
conn = getConnection();// 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);// 执行更新操作
System.out.println("操作成功,所影响的记录数为" + String.valueOf(result));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
}
// 执行更新操作并返回新添加记录的自动编号值
public int executeUpdate_id(String sql) {
int result = 0; // 定义保存返回值的变量
try {
conn = getConnection();// 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); // 执行更新操作,用于向数据表中插入数据
/***************** 获取新添加记录的自动编号字段的值 **********************/
String ID = "select @@IDENTITY as id";
rs = stmt.executeQuery(ID);
if (rs.next()) {
int autoID = rs.getInt("id");
result = autoID;
}
/******************************************************************/
} catch (SQLException ex) {
result = 0;
}
return result;// 返回保存返回值的变量
}
// 对map集执行动态UPDATE语句
public void dynamicUpdate(Map<String, String> map, String sql) {
try { // 捕捉异常
conn = getConnection();
prpdStmt = conn.prepareStatement(sql);
prpdStmt.clearBatch();
Iterator<Entry<String, String>> entryIt = map.entrySet().iterator();//a set view of the mappings contained in this map.
while (entryIt.hasNext()) {
Entry<String, String> entry = entryIt.next();
prpdStmt.setString(1, entry.getValue());
prpdStmt.setString(2, entry.getKey());
prpdStmt.addBatch();
}
prpdStmt.executeBatch();//对批次进行更新
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
}
// 执行动态DELETE语句
public void dynamicDelete(String[] ids, String sql) {
try { // 捕捉异常
conn = getConnection();
prpdStmt = conn.prepareStatement(sql);
prpdStmt.clearBatch();
for (int i = 0; i < ids.length; i++) {
prpdStmt.setString(1, ids[i]);
prpdStmt.addBatch();
}
prpdStmt.executeBatch();
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try { // 捕捉异常
if (rs != null) { // 当ResultSet对象的实例rs不为空时
rs.close(); // 关闭ResultSet对象
}
if (stmt != null) { // 当Statement对象的实例stmt不为空时
stmt.close(); // 关闭Statement对象
}
if (conn != null) { // 当Connection对象的实例conn不为空时
conn.close(); // 关闭Connection对象
}
if (prpdStmt != null) { // 当Connection对象的实例prpdStmt不为空时
prpdStmt.close(); // 关闭Connection对象
}
} catch (Exception e) {
e.printStackTrace(System.err); // 输出异常信息
}
}
}
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/users";
public static final String DBUSER = "root";
public static final String DBPASS = "root";
%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
boolean flag = false;
String name = null;
String id=request.getParameter("userid");
String password=request.getParameter("password");
%>
<%
try{
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="insert into user(userid,password) values(?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,id);
pstmt.setString(2,password);
pstmt.executeUpdate();
}catch(Exception e){System.out.println(e);}
finally{
try{
pstmt.close();
conn.close();
}catch(Exception e){
}
}
%>