-2

PLEASE HELP. I have already created the table in the database and established the JDBC connection. I have tried doing the coding but I'm unable to proceed ahead. How can I dynamically insert into the database from the text boxes? I have used two separate java classes "class regdata" (that has an object- rejObj containing the variables for all the textbox fields) and the "class jdbcconn" (FOR JDBC connection, it will call the regObj and execute the insert query)

package com.healthqat;

public class Regdata {
private int qid; 
private String uname;
private String pass;
private String rel;
private String fname;
private String mname;
private String lname;
private boolean gender;
private int pbox;
private String dob; 
private String nation;
private String city;
private int tel;
private int mob;
private String email;
private String q1;
private String a1;
private String q2;
private String a2;


public int getQid() {
    return qid;
}
public void setQid(int qid) {
    this.qid = qid;
}
public String getUname() {
    return uname;
}
public void setUname(String uname) {
    this.uname = uname;
}
public String getPass() {
    return pass;
}
public void setPass(String pass) {
    this.pass = pass;
}
public String getRel() {
    return rel;
}
public void setRel(String rel) {
    this.rel = rel;
}
public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getMname() {
    return mname;
}
public void setMname(String mname) {
    this.mname = mname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}
public boolean isGender() {
    return gender;
}
public void setGender(boolean gender) {
    this.gender = gender;
}
public int getPbox() {
    return pbox;
}
public void setPbox(int pbox) {
    this.pbox = pbox;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
public String getNation() {
    return nation;
}
public void setNation(String nation) {
    this.nation = nation;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public int getTel() {
    return tel;
}
public void setTel(int tel) {
    this.tel = tel;
}
public int getMob() {
    return mob;
}
public void setMob(int mob) {
    this.mob = mob;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getQ1() {
    return q1;
}
public void setQ1(String q1) {
    this.q1 = q1;
}
public String getA1() {
    return a1;
}
public void setA1(String a1) {
    this.a1 = a1;
}
public String getQ2() {
    return q2;
}
public void setQ2(String q2) {
    this.q2 = q2;
}
public String getA2() {
    return a2;
}
public void setA2(String a2) {
    this.a2 = a2;
}
}

and in class jdbcconn I've been told to call the rejObj (object) with the help of a for each loop and execute the insert statement. But I'm not sure how to go about with it.

  public class Jdbcconn {
public String insertRecordInTest(Regdata regObj) {
     Connection conn = getConnection();
    String sql= "insert into health_user(qid,uname,pass,rel,fname,mname,lname,gender,dob,pbox,nation,city,tel,mob,email,q1,a1,q2,a2) values()" + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
    try {
        preparedStatement = conn.prepareStatement(sql);

        preparedStatement.setInt(1, qid);
        preparedStatement.setString(2, uname);
        preparedStatement.setString(3, pass);
        preparedStatement.setString(4, rel);
        preparedStatement.setString(5, fname);
        preparedStatement.setString(6, mname);
        preparedStatement.setString(7, lname);
        preparedStatement.setString(8, gender);
        preparedStatement.setString(9, dob);
        preparedStatement.setInt(10, pbox);
        preparedStatement.setString(11, nation);
        preparedStatement.setString(12, city);
        preparedStatement.setInt(13, tel);
        preparedStatement.setInt(14, mob);
        preparedStatement.setString(15, email);
        preparedStatement.setString(16, q1);
        preparedStatement.setString(17, a2);
        preparedStatement.setString(18, q2);
        preparedStatement.setString(19, a2);


        // execute insert SQL stetement
        preparedStatement.executeUpdate();

        System.out.println("Record is inserted into table!");

    } catch (SQLException e) {

        System.out.println("error in inserting data"+ e) ;

        for (var key  regObj) {
              if (regObj.hasOwnProperty(key)) {
                alert(key + " -> " + regObj[key]);
              }
            }


    //smt = conn.createStatement();

     //stmt.executeQuery(sql);

}

public Connection getConnection(){

       Connection conn = null;
      // Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("oracle.jdbc.OracleDriver");

           //Create a connection to the database
            String serverName = "127.0.0.1";
            String portNumber = "1521";
            String sid = "XE";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            System.out.println(url);
            String username = "sys as sysdba";
            String password = "shibs";
            try{
            conn = DriverManager.getConnection(url, username, password);
            }
            catch(Exception e)
            {
                System.out.println("Connection Error: " + e);
            }
            if(conn != null)
            {
                System.out.println("connection to the database established sucessfully");
            }

       }

        catch (ClassNotFoundException e)
        {
            System.out.println("Error Class Not Found"+e);
        }

        return conn;

} }

1 Answers1

0

You are on the right track, but are missing a step. Here is the sequence: 1. create a service that connects to your JDBC class. In the service, define input variables in the Inputs tab representing the values that will be passed from your text boxes. 2. In the Event Editor ( or in your Javascript code ), call your service, and ( if using the visual Event Editor ) use Mapping to map your textbox's text property to the input variables ( or if in Javascript use the specific form.textbox.text to reference the value when calling the service ) This will allow you to call your JDBC service and pass in the textbox fields. You can see an example of a sample project doing this from github at https://github.com/kony/JavaConnectorApp