• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem about inserting into database

 
Tong Liu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,guys
I am doing B/S architecture student management system, the first step is to send the register info which is input in the html form by the user into the sql server database, but neigher the empty record in db nor the IE can't show the register info.
My source code is as follow:
(user_reg.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>注册页</title>
</head>

<body>
<form action="user_reg.jsp" method="post">
<p align="center">
User Name:<input type="text" name="uname"><br><br>
Password: <input type="password" name="upasword"><br><br>
Telephone: <input type="text" name="tel"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>

(user_reg.jsp)

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%--新用户注册--%>
<%! String name;
String pwd;
String telnum;
%>
<%
name=request.getParameter("uname");
pwd=request.getParameter("upasword");
telnum=request.getParameter("tel");
out.print(name+"<br>");
out.print(pwd+"<br>");
out.print(telnum+"<br>");
<jsp:useBean class="user_action.UserReg" id="reg" scope="application"/>
reg.register(name,pwd,telnum);
%>

(UserReg.java)

package user_action;
import java.sql.*;

public class UserReg {
private Connection con;
private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://10.16.110.73:1433;DatabaseName=stumanage";
private String un = "sa";
private String pwd = "dbdeveloper";
private int telnu;

public UserReg(){
try{
Class.forName(driverName);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}

public void register(String userName,String userPwd,String telNum){
telnu = Integer.valueOf(telNum).intValue();
try{
con = DriverManager.getConnection(url,un,pwd);
PreparedStatement pstmt = con.prepareStatement("insert into unistuff (username,userpwd,telno) values (?,?,?)");
pstmt.setString(1,userName);
pstmt.setString(2,userPwd);
pstmt.setInt(3,telnu);
pstmt.executeUpdate();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}

}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the error, is in your method register. You should use pstmt.setInt(3,telNum); instead of pstmt.setInt(3,telnu); and dont forget to try the database connection with following code :






if the connection is established, that means your problem is not in the connection. Hope this help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic