jsp表单提交到数据库,但数据库中并没有存储,有人知道是为啥吗?
<%@pagecontentType="text/html;charset=UTF-8"language="java"import="java.sql.*"%><%Str...
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>检验注册页面</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">
</head>
<body>
<br>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("userName");
String pass=request.getParameter("userPass");
%>
<%
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databaseName=gamestore";
String user = "root";
String password = "root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,user,password);
PreparedStatement sql =conn.prepareStatement("insert into user(userName,userPass)values(?,?)"); //注入数据
sql.setString(1,users);
sql.setString(2,pass);
try {
int rtn=sql.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
sql.close();
conn.close();
%>
您已注册成功!<br/><br/>
返回<a href="login.jsp">登陆</a>
</body>
</html> 展开
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>检验注册页面</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">
</head>
<body>
<br>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("userName");
String pass=request.getParameter("userPass");
%>
<%
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databaseName=gamestore";
String user = "root";
String password = "root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,user,password);
PreparedStatement sql =conn.prepareStatement("insert into user(userName,userPass)values(?,?)"); //注入数据
sql.setString(1,users);
sql.setString(2,pass);
try {
int rtn=sql.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
sql.close();
conn.close();
%>
您已注册成功!<br/><br/>
返回<a href="login.jsp">登陆</a>
</body>
</html> 展开
展开全部
看了下,代码没问题,检查下是否是数据库的原因。
把插入数据库的代码提取出来,写一个测试类,单独测试。
比如这个
package org.querydemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class QueryDemo {
/**
* @param args
*/
//驱动程序就是之前在classpath中配置的JDBC的驱动程序的JAR 包中
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
//连接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:mysql://localhost:3306/test";
//连接数据库的用户名
public static final String DBUSER = "root";
//连接数据库的密码
public static final String DBPASS = "";
public static void main(String[] args) throws Exception {
Connection con = null; //表示数据库的连接对象
Statement stmt = null; //表示数据库的更新操作
ResultSet result = null; //表示接收数据库的查询结果
Class.forName(DBDRIVER); //1、使用CLASS 类加载驱动程序
con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //2、连接数据库
stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
result = stmt.executeQuery("select name,age,address from java_study.person"); //执行SQL 语句,查询数据库
while (result.next()){
String name = result.getString("name");
int age = result.getInt("age");
String address = result.getString("address");
System.out.println(name+age+address);
}
result.close();
con.close(); // 4、关闭数据库
}
}
把插入数据库的代码提取出来,写一个测试类,单独测试。
比如这个
package org.querydemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class QueryDemo {
/**
* @param args
*/
//驱动程序就是之前在classpath中配置的JDBC的驱动程序的JAR 包中
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
//连接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:mysql://localhost:3306/test";
//连接数据库的用户名
public static final String DBUSER = "root";
//连接数据库的密码
public static final String DBPASS = "";
public static void main(String[] args) throws Exception {
Connection con = null; //表示数据库的连接对象
Statement stmt = null; //表示数据库的更新操作
ResultSet result = null; //表示接收数据库的查询结果
Class.forName(DBDRIVER); //1、使用CLASS 类加载驱动程序
con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //2、连接数据库
stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
result = stmt.executeQuery("select name,age,address from java_study.person"); //执行SQL 语句,查询数据库
while (result.next()){
String name = result.getString("name");
int age = result.getInt("age");
String address = result.getString("address");
System.out.println(name+age+address);
}
result.close();
con.close(); // 4、关闭数据库
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询