请帮忙。我已经在数据库中创建了表并建立了 JDBC 连接。我已尝试进行编码,但无法继续进行。如何从文本框中动态插入数据库?我使用了两个单独的 java 类“class regdata”(它有一个包含所有文本框字段变量的对象 rejObj)和“class jdbcconn”(对于 JDBC 连接,它将调用 regObj 并执行插入查询)
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;
}
}
在 jdbcconn 类中,我被告知在 for each 循环的帮助下调用 rejObj(对象)并执行插入语句。但我不知道该怎么做。
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;
} }