www.webdeveloper.com
Results 1 to 3 of 3

Thread: insert data into database

  1. #1
    Join Date
    Feb 2004
    Posts
    47

    Unhappy insert data into database

    i am using jsp to create web site. How can i insert data into database but do not have text box in the web site. i want to have data automatically show in the database without having people key in data from web site. the purpose for doing this is i want to generate default answer for every member.

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    You will need to use an SQL query like the following.
    Code:
    INSERT INTO tableName (fieldOne) VALUES ('valueOne');
    Here is a good article i found: http://www.roseindia.net/mysql/mysql.htm

    Regards,
    Andrew Buntine.

  3. #3
    Join Date
    Feb 2004
    Posts
    47

    Unhappy

    i want to generate answer for the user. every user will have different answer. their answer will be what is their lot no. i already generate question for them which is "what is my lot no?"
    i attach part of my code. Every data in the code are fine except "answer" part because data that insert into database was wrong. data in database under "answer" field all has same data.


    <% if (session.getAttribute("user_name") != null) {

    String uName = (String)session.getValue("user_name");
    String sql_1 = "SELECT es_iden, es_access_no "+
    "FROM esaccs "+
    "WHERE es_usr_id='"+uName+"' ";
    Statement stmt_1 = conn.createStatement();
    ResultSet rset_1 = stmt_1.executeQuery(sql_1);
    while (rset_1.next()) {
    ident = rset_1.getString("es_iden");
    accNo = rset_1.getString("es_access_no");
    //answer = rset_1.getString("es_ans");
    }

    String sql_2 = "SELECT fs_email_addr "+
    "FROM fcrsdt "+
    "WHERE fs_access_no = '"+accNo+"' ";
    Statement stmt_2 = conn.createStatement();
    ResultSet rset_2 = stmt_2.executeQuery(sql_2);
    while (rset_2.next()) {
    emailAddr = rset_2.getString("fs_email_addr");
    out.print(emailAddr);
    //lotno = rset_2.getString("fs_lot_no");
    //out.print(lotno);
    }
    String select_e = "SELECT fs_lot_no "+
    "FROM fcrsdt "+
    "WHERE fs_lot_no = '"+answer+"' ";
    Statement stmt_e = conn.createStatement();
    ResultSet rset_e = stmt_e.executeQuery(select_e);
    if (rset_e.next()) {
    answer = rset_e.getString("fs_lot_no");
    out.print(answer);
    session.setAttribute("answer", answer);
    }
    }

    try {
    int totalRecordInDatabase = 0;
    int intAccsNo = 1;
    String sql = "SELECT count(*) as recordCount "+
    "FROM fcrsdt "+
    "WHERE fs_cat_typ ='O' ";
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery(sql);
    if (rset.next()) {
    totalRecordInDatabase = rset.getInt("recordCount");
    }
    int iZero, iNine, iA, iZ, randNum;
    StringBuffer bufUserID;
    StringBuffer bufPassword;
    //StringBuffer bufQuestion;
    StringBuffer bufAnswer;

    String strAccessNo="",accessNo,userID,password, question;

    iZero = '0';
    iNine = '9';
    iA = 'A';
    iZ = 'Z';

    Random randGen = new Random(System.currentTimeMillis());
    bufUserID = new StringBuffer();
    bufPassword = new StringBuffer();
    //bufQuestion = new StringBuffer();
    //bufAnswer = new StringBuffer();

    while (intAccsNo <= totalRecordInDatabase) {
    strAccessNo = String.valueOf(intAccsNo);
    accessNo = strAccessNo+".00";

    for (int i = 0; i< 6; i++) {
    randNum = randGen.nextInt(iZ);
    while (!(randNum >= iZero && randNum <= iNine) &&!(randNum >= iA && randNum <= iZ)) {
    randNum = randGen.nextInt(iZ);
    }
    char c = (char)randNum ;
    bufUserID.append(c);
    }//for
    for (int ii= 0; ii< 6; ii++) {
    randNum = randGen.nextInt(iZ);
    while (!(randNum >= iZero && randNum <= iNine) &&!(randNum >= iA && randNum <= iZ)){
    randNum = randGen.nextInt(iZ);
    }
    char c = (char)randNum ;
    bufPassword.append(c);
    }//for

    userID = bufUserID.toString();
    password = bufPassword.toString();
    question = "What is my lot no?";
    answer = answer;
    out.print(answer);

    bufUserID.delete(0,7);
    bufPassword.delete(0,7);
    //bufQuestion.delete(0,7);
    //bufAnswer.delete(0,7);
    String sql_insert ="INSERT INTO esaccs(es_access_no,es_usr_id,es_password,es_sts,e
    s_iden,es_question,es_ans) "+
    "VALUES(?,?,?,?,?,?,?)";
    PreparedStatement ps_1 = conn.prepareStatement(sql_insert);
    ps_1.setString(1,accessNo);
    ps_1.setString(2,userID);
    ps_1.setString(3,password);
    ps_1.setString(4,"A");
    ps_1.setString(5,"O");
    ps_1.setString(6,question);
    ps_1.setString(7,answer);
    ps_1.executeUpdate();
    ps_1.close();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles