【发布时间】:2017-06-29 14:53:11
【问题描述】:
我是 java web 新手。我想编写简单的登录页面,将数据插入 MySql。当我运行代码时,一切运行正常,但是数据 int 插入到数据库中。我现在搜索了大约 2 天但无法解决。谢谢你的帮助 第一个是我的数据库类名是 MyDb,第二个是 server.Also 我的 id 是自动递增的
#1
Connection conn;
public Connection getCon(){
try {
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost:3306/sign_up", "root", "12345");
System.out.println("connection is done");
} catch (ClassNotFoundException ex) {
Logger.getLogger(MyDb.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(MyDb.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}
#2
public class registr extends HttpServlet {
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String name = request.getParameter("name");
String username = request.getParameter("username")
String password = request.getParameter("password");
String email = request.getParameter("email");
MyDb db = new MyDb();
Connection conn = db.getCon();
conn.setAutoCommit(true);
String query = "INSERT INTO sign(name, surname, email, password)" + "VALUES(?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, name);
stmt.setString(2, username);
stmt.setString(3, password);
stmt.setString(4, email);
stmt.executeUpdate(query);
} catch (SQLException ex) {
Logger.getLogger(registr.class.getName()).log(Level.SEVERE,null,ex);
}
}
【问题讨论】:
-
添加堆栈跟踪
-
没有错误码