leftMessage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'leftMessage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="/webproject1/fckeditor/fckeditor.js"></script>
</head>
<body>
<form method="post" name="login" action="/webproject1/servlet/add">
<table width="600" border="0" align="center">
<tr>
<td>姓名:<input type="text" name="name" size="40"></td>
</tr>
<tr>
<td>主题:<input type="text" name="title" size="40"></td>
</tr>
<tr>
<td>内容:</td>
</tr>
<tr>
<td>
<script>
var editor = new FCKeditor('editor1');
editor.BasePath = '/webproject1/fckeditor/';
editor.ToolbarSet = 'Default';
editor.Height = 300;
editor.Create();
</script>
</td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>DBUtil.java
package com.leftMessage.Util;import java.sql.*;public class DBUtil { public Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/jcsc";
String user = "root";
String pwd = "password";
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
return conn;
} public ResultSet select(String sql) throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null; try {
conn = getConnection();
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void insert(String sql) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
pst.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch(Exception e) {
}
try {
if (pst != null) {
pst.close();
}
} catch(Exception e) {
}
}
}
}
addServlet.java
package servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leftMessage.Util.*;
public class addServlet extends HttpServlet { private static final long serialVersionUID = -8071882983096997943L; public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String title = request.getParameter("title");
String value = request.getParameter("editor1");
String sql = "insert into message(name,title,value) values(name,title,value)";
if(name != null && name.length()>0 && title != null && title.length()>0 && value != null && value.length()>0) {
DBUtil util = new DBUtil();
util.insert(sql);
out.println("数据添加成功");
} else {
out.println("请完整输入");
}
}}
为什么mysql数据没有添加进去?谢谢
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'leftMessage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="/webproject1/fckeditor/fckeditor.js"></script>
</head>
<body>
<form method="post" name="login" action="/webproject1/servlet/add">
<table width="600" border="0" align="center">
<tr>
<td>姓名:<input type="text" name="name" size="40"></td>
</tr>
<tr>
<td>主题:<input type="text" name="title" size="40"></td>
</tr>
<tr>
<td>内容:</td>
</tr>
<tr>
<td>
<script>
var editor = new FCKeditor('editor1');
editor.BasePath = '/webproject1/fckeditor/';
editor.ToolbarSet = 'Default';
editor.Height = 300;
editor.Create();
</script>
</td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>DBUtil.java
package com.leftMessage.Util;import java.sql.*;public class DBUtil { public Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/jcsc";
String user = "root";
String pwd = "password";
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
return conn;
} public ResultSet select(String sql) throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null; try {
conn = getConnection();
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void insert(String sql) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
pst.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch(Exception e) {
}
try {
if (pst != null) {
pst.close();
}
} catch(Exception e) {
}
}
}
}
addServlet.java
package servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leftMessage.Util.*;
public class addServlet extends HttpServlet { private static final long serialVersionUID = -8071882983096997943L; public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String title = request.getParameter("title");
String value = request.getParameter("editor1");
String sql = "insert into message(name,title,value) values(name,title,value)";
if(name != null && name.length()>0 && title != null && title.length()>0 && value != null && value.length()>0) {
DBUtil util = new DBUtil();
util.insert(sql);
out.println("数据添加成功");
} else {
out.println("请完整输入");
}
}}
为什么mysql数据没有添加进去?谢谢
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>addServlet</servlet-name>
<servlet-class>servlet.addServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>addServlet</servlet-name>
<url-pattern>/servlet/add</url-pattern>
</servlet-mapping>
</web-app>
把这加
e.printStackTrace();
这样好分析问题所在。
你这sql语句有问题<servlet-mapping>
<servlet-name>addServlet</servlet-name>
<url-pattern>/servlet/add</url-pattern>
</servlet-mapping>
把打红色的servlet去掉 在把jsp页面上的也去掉