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
9 replies to this topic
#1
Posted 05 November 2010 - 03:38 PM
|
|
|
#2
Posted 06 November 2010 - 12:31 AM
What is query contain?
#3
Posted 06 November 2010 - 02:03 AM
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
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
Posted 06 November 2010 - 02:16 AM
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
Posted 06 November 2010 - 03:47 AM
Why are you getting a result set, and THEN executing the insert (obliterating the result set you just got)?
#6
Posted 06 November 2010 - 03:52 AM
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
Posted 06 November 2010 - 05:33 AM
what would be the right code,guys..
#8
Posted 06 November 2010 - 06:13 AM
add
conn.commit(); conn.close();as final 2 lines.
#9
Posted 08 November 2010 - 05:49 AM
oxano said:
add
conn.commit(); conn.close();as final 2 lines.
where do i put this?
#10
Posted 10 November 2010 - 08:37 AM
According to java doc:
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.
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


Sign In
Create Account

Back to top









