---
title: "Insert Select Into in java using Prepared statement"  
description: "Insert Select Into in java using Prepared statement"  
author: "Anonymous User"  
published: 2015-12-24  
updated: 2015-12-24  
canonical: https://www.mindstick.com/forum/33782/insert-select-into-in-java-using-prepared-statement  
category: "java"  
tags: ["java", "sql", "database connection"]  
reading_time: 2 minutes  

---

# Insert Select Into in java using Prepared statement

I am doing [INSERT INTO SELECT](https://www.mindstick.com/interview/23338/the-sql-insert-into-select-statement) which are inserting into in 1 table by selecting specific data in columns from 2 tables. But, it will involve with [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input) from JTextField as well. I have searched for many [solutions](https://www.mindstick.com/articles/12807/product-searching-issues-and-solutions-for-ecommerce-store-advanced-search-autocomplete-suggest) but still got an [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) and I just dunno what else to do. I'm using Java as PL and [Oracle](https://www.mindstick.com/articles/13016/alter-table-statement-or-command-in-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 subjectCodeString sbjN = sbjName.getText(); //textfield for subjectNameString matricsno = textstudentid.getText(); //textfield for matrics numberString 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](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) index.\
The SQL statement has been test in SQL [Developer](https://www.mindstick.com/articles/157260/variation-between-web-designer-and-web-developer) and succeed. Just I'm bit confused on how to do it on Java. Thank you for all of your [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) and time. I'm a newbie in Java.

## Replies

### Reply by Anonymous User

```
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 ()
```

Error was came from giving parameters (setString...) without matching ?


---

Original Source: https://www.mindstick.com/forum/33782/insert-select-into-in-java-using-prepared-statement

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
