Jump to content

How to use insert query in java

- - - - -

  • Please log in to reply
9 replies to this topic

#1
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
i have a hard time in using insert query for database ibm db2 using java can u pls help me guys on how to use it using step by step process

#2
vachovsky

vachovsky

    Newbie

  • Members
  • Pip
  • 8 posts
What is query contain?

#3
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
import java.sql.*;

public class ConnectionSample
{
public static void main(String[] args)
{
// CONNECTION OF DATABASE ACCESS/IBM
try{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");


String url = "jdbc:db2:sample";
String uname = "", psswrd = "";

Connection conn = DriverManager.getConnection(url, uname, psswrd);

Statement s = conn.createStatement();

//QUERY SIDE

s.executeQuery("SELECT FIRSTNME ,LASTNAME,MIDINIT FROM EMPLOYEE ");


ResultSet rset = s.getResultSet();

s.executeUpdate("INSERT INTO EMPLOYEE (FIRSTNME,LASTNAME,MIDINIT )VALUES('ROB','SEMB','R')");



System.out.print("FIRSTNAME: \t" );
System.out.print("LASTNAME: \t" );
System.out.println("MIDINIT: \t");






// EXECUTE
while(rset.next())
{

System.out.print(rset.getString(1) + " \t" );
System.out.print(rset.getString(2) + " \t" );
System.out.println(rset.getString(3)+ " ");

}

}
catch(ClassNotFoundException exp){

System.err.println(exp);
}
catch(SQLException exp)
{
System.err.println(exp);
}
}
}

//s.executeUpdate("INSERT INTO EMPLOYEE (FIRSTNME,LASTNAME,MIDINIT )VALUES('ROB','SEMB','R')");
this one ..cause it won't save in the database

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Try to close the connection in the end after a commit.

conn.commit();

conn.close();

Maybe it doesn't commit properly and rollbacks if the program ends without having closed the connection.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why are you getting a result set, and THEN executing the insert (obliterating the result set you just got)?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
He puts the result of the select statement in a ResultSet. So if the statement changes that resultset object should stay the same... unless it's by reference behind the scenes, which i doubt.

#7
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts
what would be the right code,guys..

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
add
conn.commit();
conn.close();
as final 2 lines.

#9
rob_programmer

rob_programmer

    Newbie

  • Members
  • Pip
  • 8 posts

oxano said:

add
conn.commit();
conn.close();
as final 2 lines.

where do i put this?

#10
discomonk

discomonk

    Newbie

  • Members
  • Pip
  • 7 posts
According to java doc:
public interface Statement
The object used for executing a static SQL statement and returning the results it produces.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

I suspect your resultset is open and holding up resources that prevents the statement from issuing insert. Try to use 2 statements, one for insert and one for select.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users