insert data into database

  • jilshi
  • Novice
  • Novice
  • User avatar
  • Posts: 32

Post 3+ Months Ago

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. i insert part of my code. Do you know what's wrong with that?
Code: [ Select ]
<%    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,es_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();
  1. <%    if (session.getAttribute("user_name") != null) {
  2.         
  3.           String uName = (String)session.getValue("user_name");
  4.         String sql_1 = "SELECT es_iden, es_access_no "+
  5.                       "FROM esaccs "+
  6.                       "WHERE es_usr_id='"+uName+"' ";
  7.         Statement stmt_1 = conn.createStatement();
  8.         ResultSet rset_1 = stmt_1.executeQuery(sql_1);
  9.         while (rset_1.next()) {
  10.             ident = rset_1.getString("es_iden");
  11.             accNo = rset_1.getString("es_access_no");
  12.             //answer = rset_1.getString("es_ans");
  13.         }
  14.         
  15.         String sql_2 = "SELECT fs_email_addr "+
  16.                       "FROM fcrsdt "+
  17.                       "WHERE fs_access_no = '"+accNo+"' ";
  18.         Statement stmt_2 = conn.createStatement();
  19.         ResultSet rset_2 = stmt_2.executeQuery(sql_2);
  20.         while (rset_2.next()) {
  21.             emailAddr = rset_2.getString("fs_email_addr");
  22.             out.print(emailAddr);
  23.             //lotno = rset_2.getString("fs_lot_no");
  24.             //out.print(lotno);
  25.         }
  26.         String select_e = "SELECT fs_lot_no "+
  27.                  "FROM fcrsdt "+
  28.                  "WHERE fs_lot_no = '"+answer+"' ";
  29.             Statement stmt_e = conn.createStatement();
  30.             ResultSet rset_e = stmt_e.executeQuery(select_e);
  31.             if (rset_e.next()) {
  32.                 answer = rset_e.getString("fs_lot_no");
  33.                 out.print(answer);
  34.                 session.setAttribute("answer", answer);
  35.         }
  36.     }
  37.     
  38.     try {
  39.         int totalRecordInDatabase = 0;
  40.         int intAccsNo = 1;
  41.         String sql = "SELECT count(*) as recordCount "+
  42.                      "FROM fcrsdt "+
  43.                      "WHERE fs_cat_typ ='O' ";
  44.         Statement stmt = conn.createStatement();
  45.         ResultSet rset = stmt.executeQuery(sql);
  46.         if (rset.next()) {
  47.             totalRecordInDatabase = rset.getInt("recordCount");
  48.         }
  49.         int iZero, iNine, iA, iZ, randNum;
  50.         StringBuffer bufUserID;
  51.         StringBuffer bufPassword;
  52.         //StringBuffer bufQuestion;
  53.         StringBuffer bufAnswer;
  54.         
  55.         String strAccessNo="",accessNo,userID,password, question;
  56.         
  57.         iZero = '0';
  58.         iNine = '9';
  59.         iA = 'A';
  60.         iZ = 'Z';
  61.             
  62.         Random randGen = new Random(System.currentTimeMillis());
  63.         bufUserID = new StringBuffer();
  64.         bufPassword = new StringBuffer();
  65.         //bufQuestion = new StringBuffer();
  66.         //bufAnswer = new StringBuffer();
  67.         
  68.         while (intAccsNo <= totalRecordInDatabase) {
  69.             strAccessNo = String.valueOf(intAccsNo);
  70.             accessNo = strAccessNo+".00";
  71.             
  72.             for (int i = 0; i< 6; i++) {
  73.                 randNum = randGen.nextInt(iZ);
  74.                 while (!(randNum >= iZero && randNum <= iNine) &&!(randNum >= iA && randNum <= iZ)) {
  75.                     randNum = randGen.nextInt(iZ);
  76.                 }
  77.                 char c = (char)randNum ;
  78.                 bufUserID.append(c);
  79.             }//for
  80.             for (int ii= 0; ii< 6; ii++) {
  81.                 randNum = randGen.nextInt(iZ);
  82.                 while (!(randNum >= iZero && randNum <= iNine) &&!(randNum >= iA && randNum <= iZ)){
  83.                     randNum = randGen.nextInt(iZ);
  84.                 }
  85.                 char c = (char)randNum ;
  86.                 bufPassword.append(c);
  87.             }//for
  88.             
  89.             userID = bufUserID.toString();
  90.             password = bufPassword.toString();
  91.             question = "What is my lot no?";
  92.             answer = answer;
  93.             out.print(answer);
  94.             
  95.             bufUserID.delete(0,7);
  96.             bufPassword.delete(0,7);
  97.             //bufQuestion.delete(0,7);
  98.             //bufAnswer.delete(0,7);
  99.             String sql_insert ="INSERT INTO esaccs(es_access_no,es_usr_id,es_password,es_sts,es_iden,es_question,es_ans) "+
  100.                               "VALUES(?,?,?,?,?,?,?)";
  101.             PreparedStatement ps_1 = conn.prepareStatement(sql_insert);
  102.             ps_1.setString(1,accessNo);
  103.             ps_1.setString(2,userID);
  104.             ps_1.setString(3,password);
  105.             ps_1.setString(4,"A");
  106.             ps_1.setString(5,"O");
  107.             ps_1.setString(6,question);
  108.             ps_1.setString(7,answer);
  109.             ps_1.executeUpdate();
  110.             ps_1.close();

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.