File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem about inserting into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem about inserting into database" Watch "Problem about inserting into database" New topic
Author

Problem about inserting into database

Tong Liu
Greenhorn

Joined: Jan 08, 2006
Posts: 6
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

Joined: Jan 10, 2002
Posts: 61209
    
  66

Moved to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem about inserting into database