request.setCharacterEncoding("UTF-8");String nickname = request.getParameter("nickname");
String password = request.getParameter("password");
String email = request.getParameter("email");
System.out.print(nickname + password + email);
Connection conn = DB.getConn();
String sql = "insert into logininfo values (null,?,?,?,1)";
PreparedStatement pstmt = DB.prepareStmt(conn,sql);
pstmt.setString(1,nickname);
pstmt.setString(2,password);
pstmt.setString(3,email);
System.out.println(sql);
pstmt.executeUpdate(sql);
DB.close(pstmt);
DB.close(conn);response.sendRedirect("index.jsp");
DB.java
package com.job51.www;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/job", "root", "baixingong");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.print("加载mysql数据库驱动出错");
e.printStackTrace();
}
return conn;
} public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stmt;
} public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public static PreparedStatement prepareStmt(Connection conn,String sql){
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pstmt;
} public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
} public static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
} public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null;
}
}
}JSP
解决方案 »
- JSTL 循环语句问题
- 学struts2遇到的一些问题...在线求解答.
- 有没有办法让下拉框的某一项不可选呢?(即让某一项变为灰色)
- 给多人发送邮件问题
- java window.showModalDialog 不能粘贴的问题
- 用SmartUpload上传文件到服务器,IE到底传了什么啊!假如我有用一个application,该怎么做呢?
- 急问!如何向JBUILDER中添加字体?
- 怎么样显示树结构的问题!
- JSP如何调用JAVA类??谁能给个例子?多谢!
- 大侠们救救我啊---jsp检索数据时提示:"无效的描述符索引"
- input的value值相关问题,求大神帮忙
- 怎样在Java中实现这个需要split的查询
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,1)' at line 1
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.registerok_jsp._jspService(registerok_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,1)' at line 1
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:525)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.Util.getInstance(Util.java:386)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
org.apache.jsp.registerok_jsp._jspService(registerok_jsp.java:71)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.36 logs.
你这个sql 还要的干嘛? pstmt.executeUpdate(sql);
最好把需要的表字段放在表名后面,如 insert into logininfo(id,nickname,password,email,gendex) values (null,?,?,?,1)
问题2: String sql = "insert into logininfo values (null,?,?,?,1)";
你这个Sql语句,null代表什么啊,一般第一个是id吧 可以再数据库中设置自增,你自己试试将问号都替换了去数据库执行以下语句看看能不能对这是插入语句pstmt.executeUpdate(sql); 这是更新时候用的啊,这个都不知道吗?
PreparedStatement pstmt = DB.prepareStmt(conn,sql);
pstmt.setString(1,nickname);
pstmt.setString(2,password);
pstmt.setString(3,email);
System.out.println(sql);
pstmt.executeUpdate(sql);修改成pstmt.execute()前面DB.prepareStmt(conn,sql); 这个地方已经有了
DB.close(pstmt); DB.close(conn);
把你的sql打印出来看看是不是语法错了呀