简体   繁体   中英

How to do INSERT SELECT INTO in Java using PrepareStatement

I'm trying to do an INSERT INTO SELECT which are inserting into in 1 table by selecting specific data in columns from 2 tables. The thing is, it will involve with user input from JTextField as well. I have searched for many solutions but still got an error and I just dunno what else to do. I'm using Java as PL and Oracle as DB. This is what I have got so far :

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);

The only error I have got after executing and insert all data needed into JTextField is java.sql.SQLException : Invalid column index .

The SQL statement has been test in SQL Developer and succeed. Just I'm bit confused on how to do it on Java. Thank you for all of your response and time. I'm a newbie in Java.

For PreparedStatement, you'd code ? Into the sql and later replace that with values.

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,matricsno);

ps.executeUpdate ();

This should do it.

Your error came from giving parameters (setString...) without matching ?

Unfortunately you absolutely misunderstood what the preparedStatemet does. You have to write ? -s into the query and the preparedSatement is going to replace it on a typed way:

 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();

To answer my own question, I'll put comment on the changes that I have made to the code and make it work:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM