How can I insert a record through a java bean

10-11  Source: Network gathering  Views:0 

Advertisement
Hi everybody,
I'd some problem when I insert a record using java bean. When I want to insert a record. The result in executeUpdate must return 0. Would you please tell me what's wrong in my code?
java bean
package miniproj;
import java.sql.*;
public class Register{
     private String sql;
     private String username;
     private String usertype;
     private String password;
     private String blocknum;
     private int floornum;
     private String owner;
     private int rows;
     private String s;
// set parameter to database
     public int setMember(){
          try{
//load database driver
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Get a connection to the database
               Connection sqlConn = java.sql.DriverManager.getConnection("jdbc.odbc:WFBS");
// Get a statement from the connection
               Statement sqlStmt = sqlConn.createStatement();
               sql = "INSERT INTO USERS(user_name, password) VALEUS ('aaa', 'aaa')";
//               sql = "INSERT INTO USERS(user_name, password) VALEUS (" + user_name + "," + password + ")" ;
               rows = sqlStmt.executeUpdate(sql);
//               sqlRst.close();
               sqlStmt.close();
               sqlConn.close();
          catch(SQLException se){
          catch(Exception e){
          return rows;
// set user name method
     public void setUsername(String name){
          username = name;
// set passwrod method
     public void setPassword(String pwd){
          password = pwd;
// set block num mehtod
     public void setBlockNum(String block){
          blocknum = block;
// set floor num method
     public void setFloorNum(int floor){
          floornum = floor;
// set owner method
     public void setOnwer(String own){
          owner = own;
// get user name method
     public String getUsername(){
          return username;
// get password method
     public String getPassword(){
          return password;
jsp code
<%@ include file="Header.jsp" %>
<%@ page errorPage="Error.jsp" %>
<jsp:useBean id="register" class="miniproj.Register" />
<jsp:setProperty name="register" property="*" />
User name : <%=register.getUsername()%><br>
Password : <%=register.getPassword()%><br>
<%
     int i = register.setMember();
     out.println(i);
%>
Thanks a lot
Chris
Correct the spelling of Values
sql = "INSERT INTO USERS(user_name, password) VALUES ('aaa', 'aaa')";
Related articles