• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem about inserting into database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}

}
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic