I am trying to get Generated key from sequnce.(Using Servlets & Oracle10)
Following is my code:
query ="insert into TABLE_NAME(COL1,COL2,COL3) values(sysdate,?,SEQ_NAME.nextval)";
PreparedStatement pstmt = con.prepareStatement(query,new String[]{"COL3"}); //Getting error on this line
pstmt.setString(1,Str2);
pstmt.executeUpdate();
ResultSet keyset = pstmt.getGeneratedKeys();
if(keyset.next())
{
genKey = keyset.getString(1);
}
But I am getting the Exception:
java.sql.SQLException: Unsupported feature
Few days ago this code was working fine. So what might be the reason that this code is not working now? (I haven't changed the JDBC driver war file)
Thanks in advance.
Is there another way of getting the value generated by sequence in the query?
Download the latest JDBC drivers, I think you need at least the 10.2.0.1 drivers and the db also need to be 10.2+
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc101040.html
or first get the sequence value
String sqlForSeq = "select SEQ_NAME.NEXTVAL from dual";
ResultSet rs = stmt.executeQuery(sqlForSeq);
if (rs.next()) {
logSeq = rs.getString("NEXTVAL");
}
Related
I am having this problem,
When I send an SQL query as an argument it gives error
when I use the same query directly in my java program it works fine.
MY SQL query when I send as an argument is as follow
Select RATINGPERIOD from INVESTMENT.I1INVE Where INVESTMENTID = 100
rs = stmt.executeQuery(sqlQery); // Select RATINGPERIOD from INVESTMENT.I1INVE Where INVESTMENTID = 100
and when I use it directly into my java program is as follow
try
{
// Load the driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("**** Loaded the JDBC driver");
// Create the connection using the IBM Data Server Driver for JDBC and SQLJ
con = DriverManager.getConnection (url, user, password);
// Create the Statement
stmt = con.createStatement();
System.out.println("**** Created JDBC Statement object");
// Execute a query and generate a ResultSet instance
rs = stmt.executeQuery("Select RATINGPERIOD from INVESTMENT.I1INVE where INVESTMENTID = 100");
while (rs.next()) {
delay = rs.getString("RATINGPERIOD");
System.out.println("size of list ="+delay);
}
}
Error log
com.ibm.db2.jcc.am.SqlException: [jcc][10103][10941][3.62.57] Method executeQuery cannot be used for update. ERRORCODE=-4476, SQLSTATE=null
at com.ibm.db2.jcc.am.fd.a(fd.java:660)
at com.ibm.db2.jcc.am.fd.a(fd.java:60)
at com.ibm.db2.jcc.am.fd.a(fd.java:120)
at com.ibm.db2.jcc.am.jn.a(jn.java:4129)
at com.ibm.db2.jcc.am.jn.a(jn.java:2875)
at com.ibm.db2.jcc.am.jn.a(jn.java:679)
at com.ibm.db2.jcc.am.jn.executeQuery(jn.java:663)
at com.profitsoftware.testing.utils.keywords.date.DatabaseSQL.sqlQuery(DatabaseSQL.java:46)
at com.profitsoftware.testing.utils.keywords.date.DatabaseSQLKeywords.executeSqlQuery(DatabaseSQLKeywords.java:18)com.
ok some more information, I have assigned sql query to a string in Java program and then compared it to the query(String) I was getting as argument in my java program and it gives false. which explain maybe when query is passed from RIDE to Java programm it changes somehow. any idea what happens there ?
Thanks in advance and sorry if it sounds a stupid question, I a new to this programming world.
oK, It started to work, actually I was missing something there, so type. My logic itself was good but it was typo that created the problem
--Sara
I am trying to run a simple query to fetch data using Java 1.6 and Oracle 9i.
Even though I am getting the result with a statement but while using preaparedstatement result set returned with empty rows. Have any body faced the same issue?
My JDBC driver version is 10.1.0.5.0
and DB is Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
Following is the program.
String query = "SELECT count(SEVERITY) as CNT FROM TICKET_TABLE where upper(customerid)=?" ;
PreparedStatement pst =null;
ResultSet lResultSet = null;
pst = con.prepareStatement(query);
pst.setString(1, "CUST_A");
lResultSet = pst.executeQuery();
while(lResultSet.next()) {
i = lResultSet.getInt("CNT");
}
lResultSet.close();
pst.close();
con.close();
You can check the compatibility of the oracle driver you are using with the java version and database version.
Secondly, confirm that the query criteria suppose to return records by running it first through any Oracle client tool.
I have tried several examples , but none of them seem to work. Here is the code I tried last
import oracle.jdbc.driver.OracleDriver;
PreparedStatement prs = null;
ResultSet rrs = null;
Class stmt1 = null;
java.lang.reflect.Field mem = null;
requestSQL = "Select FIPS_STATE_CD_TXT, FIPS_COUNTY_CD_TXT from MSTR_FIPS_COUNTY where STATE_ID = ? " + " and COUNTY_TXT = ?";
prs.setString(1, vPropertyState);
prs.setString(2, vPropertyCounty);
System.out.println(prs.toString()); //JRN
Class stmt1 = prs.getClass();
java.lang.reflect.Field mem = stmt1.getField("sql");
String value= (String)mem.get(prs);
rrs = prs.executeQuery();
I get an error on this at :
Exception trying to make a TAF call
java.lang.NoSuchFieldException: sql
at java.lang.Class.getField(Class.java:1520)
I even tried using this example from JavaWorld, but my compiler doesn't seem to recognize DebugLevel and StatementFactory. Is there a special package I should download for this?
http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html?page=3
I am using Java 1.6 and Oracle 11g. I am also looking for a quick fix, rather than installing log4jdbc or p6sy
Different drivers use different names. In your case the sql field you are trying to access is not one of the available ones for that driver.
To get all the names of your JDBC driver use this code:
Class stmt1 = prepStmt.getClass();
try {
java.lang.reflect.Field mem[] = stmt1.getDeclaredFields();
for (Field x:mem){
System.out.println("Field:"+x.getName());
}
} catch (SecurityException ex) {
}
Observe the field name, and then use your code above to print its value.
The answer is, you can't. Atleast not for the Oracle jdbc driver.
What you are trying to achieve is not possible for oracle jdbc driver.
But in general some drivers (mysql) supports this
prs.toString()
my first time in eclipse and im trying to get some test data from my sql server,now the problem is i have setup a sql connection using ms jdbc drivers and it seems like it works but when i run my query from eclipse,i get
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name
'KategoriName'.
error.My query works fine in sql manager.What could be the problem?I'm adding the code below as well:
String connectionString = "jdbc:sqlserver://192.168.0.155;user=user;password=password";
Connection conn= DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs;
String sqlconn="select [KategoriName] from [FINSAT6G9].[TBL_Test] whereID=493";
rs = stmt.executeQuery(sqlconn);
String aa = rs.getString("KategoriName");
System.out.println(aa);
Cheers.
Try including the database name in the connection string as so:
String connectionString = "jdbc:sqlserver://192.168.0.155;user=user;password=password;databaseName=FINSAT6G9";
Because, obviously, the error is telling you that the KategoriName column doesn't exist which can only mean 2 things: You have a typo or you are attempting to get the data from the wrong place, either the wrong database or the wrong table.
When I create a prepared statement like this in java (using JDBC):
pStmt = conn.prepareStatement(qry);
everything works ok. However when I want a scrollable resultset and use this:
pStmt = conn.prepareStatement(qry,ResultSet.TYPE_SCROLL_INSENSITIVE);
I get a syntax error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"
I'm not even using RETURNING in my query.
Any ideas?
Any help would be appreciated. Thanks
Update:
It seems to work if I use this:
pStmt = db.prepareStatement(qry,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
What is the difference between SENSITIVE and INSENSITIVE?
Thanks
The second parameter to prepareStatement should be one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS.
I guess you want to use
PreparedStatement prepareStatement(String sql,
int resultSetType,
int resultSetConcurrency)
ResultSet.TYPE_SCROLL_INSENSITIVE : This assumes that the result set does not “sense” database changes that occurred after execution of the query.
ResultSet.TYPE_SCROLL_SENSITIVE : picks up changes in the database that occurred after execution of the query