如何使用 JDBC 在数据库中插入参考值?

发布于 2024-11-30 19:49:00 字数 2446 浏览 1 评论 0原文

我尝试使用下面的代码将值插入 Oracle 10g 数据库,但在执行时出现“缺少表达式”错误。我必须将参考值传递到插入子句中,但不知道确切的语法或执行方式。 请帮助我解决这个问题。 谢谢。

学生表:-

                      Sid       VARCHAR2(200) PRIMARY KEY  CHECK(Sid>0),
                      Pass_word VARCHAR2(10) NOT NULL,
                      S_name    VARCHAR2(20) NOT NULL,
                      G_name    VARCHAR2(20)         ,
                      Branch    VARCHAR2(10) NOT NULL,
                      D_company VARCHAR2(20)         ,
                      B_Percent INT NOT NULL CHECK(B_Percent<100),
                      twelth_percent INT NOT NULL CHECK(twelth_percent<100),
                      tenth_percent INT NOT NULL  CHECK(tenth_percent<100),
                      Certify   VARCHAR2(30),
                      Semester  INT NOT NULL CHECK(Semester<9),
                      D_Birth   DATE NOT NULL,
                      Sex       VARCHAR2(6) NOT NULL

代码:

Connection connection = null;

try
{

  // Load the JDBC driver

   String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";

   Class.forName(driverName);

    connection = DriverManager.getConnection("jdbc:odbc:placement","siddharth","sid");

    studentID = StudentID.getText();
    spassword = PasswordField.getPassword();
    studentname = NameField.getText();
    Gname = GuardianField.getText();
    branch = BranchField.getText();
    dcompany = DcompanyField.getText();
    bpercent = BtechField1.getText();
    twelthpercent = TwelthField.getText();
    tenthpercent = TenthField.getText();
    semester = SemesterField.getText();
    certify = CertificationField.getText();
    sex = SexCombo.getActionCommand();
    date =  (Date) DateTextField1.getValue();



    Statement stmt = connection.createStatement();
    stmt.executeUpdate("insert into student " +"(sid,pass_word,s_name,g_name,branch,d_company,b_percent,twelth_percent,tenth_percent,certify,semester,d_birth,sex)"+
                 "values(studentID, spassword,studentname,Gname,branch,dcompany,bpercent,twelthpercent,tenthpercent,certify,semester,date,sex)" );

    stmt.close();
    connection.close();
    }

    catch (ClassNotFoundException e) {
    // Could not find the database driver
      JOptionPane.showMessageDialog(null,e);
    } 
    catch (SQLException e) {
    // Could not connect to the database
      JOptionPane.showMessageDialog(null,e);
     } 

I am trying to insert values into Oracle 10g database using the code below but its giving an error of "Missing Expression" on execution. I have to pass reference values into the insert clause but do not know the exact syntax or way of doing it.
Please help me through this.
Thanks.

Student Table:-

                      Sid       VARCHAR2(200) PRIMARY KEY  CHECK(Sid>0),
                      Pass_word VARCHAR2(10) NOT NULL,
                      S_name    VARCHAR2(20) NOT NULL,
                      G_name    VARCHAR2(20)         ,
                      Branch    VARCHAR2(10) NOT NULL,
                      D_company VARCHAR2(20)         ,
                      B_Percent INT NOT NULL CHECK(B_Percent<100),
                      twelth_percent INT NOT NULL CHECK(twelth_percent<100),
                      tenth_percent INT NOT NULL  CHECK(tenth_percent<100),
                      Certify   VARCHAR2(30),
                      Semester  INT NOT NULL CHECK(Semester<9),
                      D_Birth   DATE NOT NULL,
                      Sex       VARCHAR2(6) NOT NULL

CODE:

Connection connection = null;

try
{

  // Load the JDBC driver

   String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";

   Class.forName(driverName);

    connection = DriverManager.getConnection("jdbc:odbc:placement","siddharth","sid");

    studentID = StudentID.getText();
    spassword = PasswordField.getPassword();
    studentname = NameField.getText();
    Gname = GuardianField.getText();
    branch = BranchField.getText();
    dcompany = DcompanyField.getText();
    bpercent = BtechField1.getText();
    twelthpercent = TwelthField.getText();
    tenthpercent = TenthField.getText();
    semester = SemesterField.getText();
    certify = CertificationField.getText();
    sex = SexCombo.getActionCommand();
    date =  (Date) DateTextField1.getValue();



    Statement stmt = connection.createStatement();
    stmt.executeUpdate("insert into student " +"(sid,pass_word,s_name,g_name,branch,d_company,b_percent,twelth_percent,tenth_percent,certify,semester,d_birth,sex)"+
                 "values(studentID, spassword,studentname,Gname,branch,dcompany,bpercent,twelthpercent,tenthpercent,certify,semester,date,sex)" );

    stmt.close();
    connection.close();
    }

    catch (ClassNotFoundException e) {
    // Could not find the database driver
      JOptionPane.showMessageDialog(null,e);
    } 
    catch (SQLException e) {
    // Could not connect to the database
      JOptionPane.showMessageDialog(null,e);
     } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-12-07 19:49:00

当前您的 SQL 语句如下所示:

insert into student (sid, pass_word, ...) values (studentID, spassword, ...)

变量名称对 SQL 本身没有任何意义。

您应该使用准备好的语句。例如,您的 SQL 应如下所示:

insert into student (sid, pass_word, ...) values (?, ?, ...)

然后您使用:

PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, studentID);
stmt.setString(2, spassword);

// etc

请参阅 有关准备好的语句的 JDBC 教程了解更多信息。

Currently your SQL statement looks like:

insert into student (sid, pass_word, ...) values (studentID, spassword, ...)

The variable names mean nothing to the SQL itself.

You should use a prepared statement. For example, your SQL should look like this:

insert into student (sid, pass_word, ...) values (?, ?, ...)

and then you use:

PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, studentID);
stmt.setString(2, spassword);

// etc

See the JDBC tutorial on prepared statements for more information.

沫雨熙 2024-12-07 19:49:00

为了安全起见,您应该使用准备好的语句。

准备查询字符串

String updateString = "update " + dbName + ".COFFEES " +
                      "set SALES = ? where COF_NAME = ?";

使用此查询创建一个 PrepartedStatement:

updateSales = connnection.prepareStatement(updateString);

填充 ?带有您值的空格符:

updateSales.setInt(1, myVariable1);
updateSales.setString(2, myVariable2);

执行查询

updateSales.executeUpdate();

To be on the safe side you should use prepared statements.

Prepare the Query String

String updateString = "update " + dbName + ".COFFEES " +
                      "set SALES = ? where COF_NAME = ?";

Create a PrepartedStatement with this query:

updateSales = connnection.prepareStatement(updateString);

Fill the ? spaceholders with you values:

updateSales.setInt(1, myVariable1);
updateSales.setString(2, myVariable2);

execute the Query

updateSales.executeUpdate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文