日期:2014-05-17  浏览次数:20795 次

请教下 JSP页面向Mysql里插入数据时候 不报错,但是就是插不进去·· 求助啊
请教下 JSP页面向Mysql里插入数据时候 不报错,但是就是插不进去·· 求助啊
我的代码如下



<%@ page language="java" import="java.util.* , java.sql.*" pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("GB18030");
String User = request.getParameter("username");
String Pass = request.getParameter("password");

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:7777/ding","root","123456");  
//Statement stm = conn.createStatement();
//ResultSet rs = stm.executeQuery("select * from tuceng where username ="+"."+User+".");
try{
PreparedStatement pstm = conn.prepareStatement("insert into tuceng (username,password) values (?,?)");
pstm.setString(1,User);
pstm.setString(2,Pass);}catch(SQLException e) {
System.out.println("数据没有插入上");
      out.println(e);
    }

conn.close();
//stm.close();
//rs.close();
 %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'regresult.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
    注册成功<br>
  </body>
</html>

------解决方案--------------------
你的代码缺少很多
con.setAutoCommit(false);// 更改JDBC事务的默认提交方式 

pstm. excuteUpdate()

con.commit();//提交JDBC事务
con.setAutoCommit(true);// 恢复JDBC事务的默认提交方式 
con.close()
------解决方案--------------------
你把sql准备好了并没有执行。
拼完sql的参数之后添加执行。
pstm.execute(); 返回影响的行数列、
------解决方案--------------------
引用:
请教下 JSP页面向Mysql里插入数据时候 不报错,但是就是插不进去·· 求助啊
我的代码如下



<%@ page language="java" import="java.util.* , java.sql.*" pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("GB18030");
String User ……

Debug打节点调试下  看到当页面提交时,到底有没有调用你的方法,到底有没有提交事务
------解决方案--------------------
看了你提供的代码 是少了 pstm. excuteUpdate();
加上代码后调试下是否可以?是否有执行到这段代码。