如何使用PrepareStatement在Java中INSERT SELECT INTO

时间:2015-12-23 18:11:13

标签: java oracle jdbc prepared-statement

我试图通过从2个表中的列中选择特定数据来插入1表中的INSERT INTO SELECT。问题是,它还将涉及来自JTextField的用户输入。我已经搜索了许多解决方案,但仍然出现错误,我只是不知道还有什么可做的。我使用Java作为PL,使用Oracle作为DB。这是我到目前为止所得到的:

Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ghost","slayer");

stmt = con.createStatement();

String sbjC = sbjCode.getText(); //textfield for subjectCode
String sbjN = sbjName.getText(); //textfield for subjectName
String matricsno = textstudentid.getText(); //textfield for matrics number
String sbjG = sbjGrade.getText(); //textfield for subjectGrade (not gonna be use in db, just for comparison)

String sql1 = "INSERT INTO transferred (subjectCode,subjectName,credit,prequisite,matricsNo) "
    + "SELECT b.subjectCode,b.subjectName,b.credit,b.prequisite,s.matricsNo "
    + "FROM bitm b, student s "
    + "WHERE b.subjectCode = '"+sbjC+"' AND b.subjectName = '"+sbjN+"' AND s.matricsNo = '"+matricsno+"'";

/* table Transferred has 5 column which are subjectCode,subjectName,credit,prequisite,matricsNo [matricsno as FK]
 * table bitm has 5 column [subjectCode as PK]
 * table student has 6 column [matricsno as PK]
 */

ps = con.prepareStatement(sql1);

ps.setString(1, sbjC);
ps.setString(2, sbjN);
ps.setString(3, "SELECT credit FROM bitm WHERE subjectCode = '"+sbjC+"' AND subjectName = '"+sbjN+"'");
ps.setString(4, "SELECT prequisite FROM bitm WHERE subjectCode = '"+sbjC+"' AND subjectName = '"+sbjN+"'");
ps.setString(5, "SELECT matricsno FROM student WHERE matricsno = '"+matricsno+"'");

ps.executeUpdate(sql1);

执行并将所有数据插入JTextField后,我遇到的唯一错误是 java.sql.SQLException:列索引无效

SQL语句已在SQL Developer中测试并成功。我对如何在Java上做这件事感到困惑。 感谢您的所有回复和时间。 我是Java的新手。

3 个答案:

答案 0 :(得分:2)

对于PreparedStatement,你有代码吗?进入sql,然后用值替换它。

A.'

这应该这样做。

您的错误来自于给出参数(setString ...)而没有匹配?

答案 1 :(得分:0)

不幸的是,你绝对误解了preparedStatemet的作用。 你要写吗? -s进入查询,preparedSatement将以类型化的方式替换它:

 String sql1 = "INSERT INTO transferred (subjectCode,subjectName,credit,prequisite,matricsNo) "
                    + "SELECT b.subjectCode,b.subjectName,b.credit,b.prequisite,s.matricsNo "
                    + "FROM bitm b, student s "
                    + "WHERE b.subjectCode = ? AND b.subjectName = ? AND s.matricsNo = ?";

            ps = con.prepareStatement(sql1);
            ps.setString(1, sbjC);
            ps.setString(2, sbjN);
            ps.setString(3, matNo);
            ps.executeUpdate();

答案 2 :(得分:0)

要回答我自己的问题,我会对我对代码所做的更改发表评论并使其有效:

Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","aza","jaiza");

stmt = con.createStatement();

String sbjC = sbjCode.getText();
String sbjN = sbjName.getText();
String matricsno = textstudentid.getText();
String sbjG = sbjGrade.getText();

String sql1 = "INSERT INTO transferred (subjectCode,subjectName,credit,prequisite,matricsNo) "
        + "SELECT b.subjectCode,b.subjectName,b.credit,b.prequisite,s.matricsNo "
        + "FROM bitm b, student s "
        + "WHERE b.subjectCode = ? AND b.subjectName = ? AND s.matricsNo = ?"; // from textfield to ?

ps = con.prepareStatement(sql1);

ps.setString(1, sbjC);
ps.setString(2, sbjN);
ps.setString(3, matricsno);

//from all the SELECT statement to just 3 user-input-from-textfield column

ps.executeUpdate(); // remove the sql1 in ps.executeUpdate(sql1);