0

I'm trying to insert skeleton data into a database using jdbc.

So far my code is:

   Statement st=con.createStatement();
   String sql = "INSERT INTO student (studentid, titleid, forename, familyname, dateofbirth) "
                    + "VALUES (1, 1, 'forename1', 'surname1', '1996-06-03');";

I need to create 100 entries for this and I'm not too sure how to go about doing it. All I want is the student id, titleid, forename and familyname to increment by 1 until it reaches 100 entries with those rows filled in, date of birth doesn't need to be altered. I'm asking how to do a loop for this

5
  • how do you create the entries . please explain your problem more clearly Commented Oct 29, 2014 at 12:34
  • You have an 'edit' button under the question. You can edit and add the code that you forgot and all the other information. Commented Oct 29, 2014 at 12:38
  • You need to add duplicates of the first row 99 times right..? Can't understand your problem claerly.. Need more explanation.. Commented Oct 29, 2014 at 12:39
  • I've updated the description hopefully that makes sense Commented Oct 29, 2014 at 12:41
  • Parameterised (prepared) statements, batches, or COPY. Commented Oct 29, 2014 at 12:44

2 Answers 2

2

General answer - You should use PrepareStatement instead of Statement and execute as batch.

Common way to insert multiple entry or execute

String sql = "INSERT INTO student (studentid, titleid, forename, familyname, dateofbirth) "
                    + "VALUES (?, ?, ?, ?, ?);";
ps = connection.prepareStatement(SQL_INSERT);
for (int i = 0; i < entities.size(); i++) {
    ps.setString(1, entity.get...());
    ...
    ps.addBatch();
}
ps.executeBatch();

Important Note:

  1. Why you should use PrepareStatement Over Statement

  2. SQL Injection Example

Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks and how do I use setDate to increment by variable i each time?
setDate function accept java.util.Date, and find How can I increment a date by one day in Java?
0

There are two ways to do this. You can put your insert query inside a loop or you can put your loop inside an insert query. I have found that the better method depends on the database engine (but I've never used postresql) and the number of records you want to insert. You could bump up against the maximun query length or number of parameters or something.

The following code sample is ColdFusion, but it is intended to show the general idea of having a loop inside a query. You would have to write equivalent code in java.

<cfquery>
insert into yourtable
(field1
, field2
, etc)
select null
, null
, null
from SomeSmalllTable
where 1 = 2
<cfloop>
union
select <cfqueryparam value="#ValueForField1#">
, <cfqueryparam value="#ValueForField#">
, etc
</cfloop>
</cfquery>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.