Question #1
Could you anyone please tell me what is the benefit I will receive using the following code, when sPhoneExt is null ?
if (sPhoneExt == null || sPhoneExt.trim().equals("")) {
stmt.setNull(9, java.sql.Types.INTEGER);
} else {
stmt.setString(9, sPhoneExt);
}
Instead of stmt.setString(9, sPhoneExt);
Because iPhoneType = rset.getInt("phone_type"); will return 0 if the value is SQL NULL; which I don't want.
Question #2
And just curious stmt.setString(9, null) is performed what will be the return of rset.getInt("phone_type")?
Answer #2
getInt() will return zero when it is null in DB. You have to use the below code to know about DB NULL.
if (rs.wasNull()) {
// handle NULL field value
}
I don't see any benefit using setNull in this String case.
It is only used to check empty string "" and insert null in DB. But for that also we can do it like stmt.setString(9, null);
But when sPhoneExt is Integer and holding null, then We cannot perform
stmt.setInt(9, sPhoneExt); since setInt(int, int) API performs; converting (Unboxing) sPhoneExt (Integer) to primitive (int), so you will get NullPointerException. So you are in need of stmt.setNull(9, java.sql.Types.INTEGER);
Finally if you have inserted null in DB for NUMBER (sql type) column; getInt() will return 0 only.
This is irrespective of the below null set mechanism.
stmt.setString(9, null);
stmt.setNull(9, java.sql.Types.INTEGER)
Also Somebody told when the DB NUMBER column has default Value; that default value will be consider differently by the above two lines. But that is not true. Even that case also both the above line performs same way. It is setting NULL value; not the default value.
create table t1 (id number default 1 );
insert into t1 (id) values (2);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestDB {
public static void main(String args[]) {
PreparedStatement stmt = null;
ResultSet rs = null;
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:#10.201.32.92:1521:psprd1", "username", "password");
String query = null;
String l = null;
ResultSet rset = null;
int paramIndex = 1;
query = "UPDATE t1 " + " SET id = ?";
stmt = con.prepareStatement(query);
stmt.setInt(paramIndex++, null);
// stmt.setNull(1, java.sql.Types.INTEGER);
stmt.executeUpdate();
stmt.close();
query = "select id from t1 ";
stmt = con.prepareStatement(query);
rset = stmt.executeQuery();
rset.next();
System.out.println(rset.getString("id"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Related
I have the following method that I use with Spring JDBC
public String getState() {
String stateLink = template.queryForObject(
"select state_url from state_scrape_queue where in_use = false ORDER BY scrape_timestamp NULLS FIRST LIMIT 1",
(result, rowNum) -> {
return result.getString("state_url");
});
return stateLink;
}
I can't find an example of how to do a for update with Spring JDBC. I want in_use to be set to true using for update.
I need to use select for update since this application will be used in a multi-threaded fashion. I don't want more than one thread to get the same row and the way to prevent that is by using select for update
I was able to do this with plain JDBC, here is the question I asked how to do it with plain JDBC
select "for update" with JDBC?
Anyone know how this would be done?
This is what I came up with, feel free to recommend improvements
public String getState() throws SQLException {
String state = null;
Connection conn = DataSourceUtils.getConnection(template.getDataSource());
try {
conn.setAutoCommit(false);
String[] colNames = { "id", "state_url", "in_use" };
String query = "select " + Stream.of(colNames).collect(Collectors.joining(", "))
+ " from state_scrape_queue where in_use = false ORDER BY scrape_timestamp NULLS FIRST LIMIT 1 FOR UPDATE";
System.out.println(query);
try (Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
// Get the current values, if you need them.
state = rs.getString(colNames[1]);
rs.updateBoolean(colNames[2], true);
rs.updateRow();
conn.commit();
}
}
} catch (SQLException e) {
conn.setAutoCommit(true);
e.printStackTrace();
} finally {
conn.setAutoCommit(true);
}
return state;
}
I'm trying to get values from resulset, but it return nothing.
When i'm trying to do it through plain sql it return some values.
List<String> res = new ArrayList<String>();
try {
String query = "SELECT COLUMN_NAME FROM idoc.columns_to_show where user = ? "
+ DAO.DB2_UR_POSTFIX;
Connection connection = Properties.getDocsConnection();
try {
PreparedStatement pr = connection.prepareStatement(query);
try {
pr.setString(1, user.getDomainName());
ResultSet rs = pr.executeQuery();
try {
while (rs.next()) {
res.add(rs.getString("COLUMN_NAME"));
}
} finally {
rs.close();
}
} finally {
pr.close();
}
} finally {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return res;
Don't use column name "user" it is reserved name for DB2 database.
Therefore I couldn't find any result.
I am trying to add value into clob as give on h2 website using preparedstatement.setcharacterstream(arg,reader) null value is inserted in the database
Connection con=getConnection();
pstmnt=con.prepareStatement(sql);
int paramcounter=1;
for (Object object : param) {
if(paramcounter==1) {
int len=object.toString().length();
Reader rdr=new StringReader(object.toString());
pstmnt.setCharacterStream(1, rdr,len);;
}
else{
pstmnt.setObject(paramcounter, object);
}
paramcounter++;
}
int retvalue=pstmnt.executeUpdate();
closeConnection();
We don't really have enough information to clearly identify why you might be having difficulty, but the following code works for me:
package h2test;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.*;
public class H2testMain {
public static void main(String[] args) {
Connection con = null;
try {
// create an in-memory database ("...mem:...")
con = DriverManager.getConnection("jdbc:h2:mem:");
Statement s = null;
try {
s = con.createStatement();
s.execute("CREATE TABLE Table1 (Column1 CLOB)");
InputStream is = new FileInputStream("C:/__tmp/MIME/stuff.txt");
Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
PreparedStatement ps = con.prepareStatement(
"INSERT INTO Table1 (Column1) VALUES (?)");
ps.setCharacterStream(1, rdr);
ps.executeUpdate();
ResultSet rs = s.executeQuery(
"SELECT Column1 FROM Table1");
int rowNumber = 0;
while (rs.next()) {
String str = rs.getString("Column1");
System.out.println(
String.format(
"Row %d: CLOB is %d character(s) long.",
++rowNumber,
str.length()));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
It reads the text file
02/15/2014 10:55 358,785 stuff.txt
writes the file contents to a CLOB column in an H2 database, reads the data back from the database, and produces the following console output:
Row 1: CLOB is 358785 character(s) long.
My question is very much like Getting the return value of a PL/SQL function via Hibernate
I have a function which does some modifications internally and it returns a value.
The original idea was to do something like this:
protected Integer checkXXX(Long id, Long transId)
throws Exception {
final String sql = "SELECT MYSCHEMA.MYFUNC(" + id + ", "
+ transId + ") FROM DUAL";
final BigDecimal nr = (BigDecimal) this.getHibernateTemplate()
.getSessionFactory().getCurrentSession().createSQLQuery(sql)
.uniqueResult();
return nr.intValue();
}
Unfortunately this doesn't work with Oracle. What is the recommended way to do something like this?
Is there a way to extract declared variables from within my statement?
Hibernate Session provides a doWork() method that gives you direct access to java.sql.Connection. You can then create and use java.sql.CallableStatement to execute your function:
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
int result = call.getInt(1); // propagate this back to enclosing class
}
});
You have the following options:
With a #NamedNativeQuery:
#org.hibernate.annotations.NamedNativeQuery(
name = "fn_my_func",
query = "{ ? = call MYSCHEMA.MYFUNC(?, ?) }",
callable = true,
resultClass = Integer.class
)
Integer result = (Integer) entityManager.createNamedQuery("fn_my_func")
.setParameter(1, 1)
.setParameter(2, 1)
.getSingleResult();
With JDBC API:
Session session = entityManager.unwrap( Session.class );
final AtomicReference<Integer> result =
new AtomicReference<>();
session.doWork( connection -> {
try (CallableStatement function = connection
.prepareCall(
"{ ? = call MYSCHEMA.MYFUNC(?, ?) }"
)
) {
function.registerOutParameter( 1, Types.INTEGER );
function.setInt( 2, 1 );
function.setInt( 3, 1 );
function.execute();
result.set( function.getInt( 1 ) );
}
} );
With a native Oracle query:
Integer result = (Integer) entityManager.createNativeQuery(
"SELECT MYSCHEMA.MYFUNC(:postId, :transId) FROM DUAL")
.setParameter("postId", 1)
.setParameter("transId", 1)
.getSingleResult();
Yes, you do need to use an out parameter. If you use the doWork() method, you'd do something like this:
session.doWork(new Work() {
public void execute(Connection conn) {
CallableStatement stmt = conn.prepareCall("? = call <some function name>(?)");
stmt.registerOutParameter(1, OracleTypes.INTEGER);
stmt.setInt(2, <some value>);
stmt.execute();
Integer outputValue = stmt.getInt(1);
// And then you'd do something with this outputValue
}
});
Alternative code :)
if you want to direct result you can use below code
int result = session.doReturningWork(new ReturningWork<Integer>() {
#Override
public Integer execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
return call.getInt(1); // propagate this back to enclosing class
}
});
http://keyurj.blogspot.com.tr/2012/12/dowork-in-hibernate.html
public static void getThroHibConnTest() throws Exception {
logger.debug("UsersActiion.getThroHibConnTest() | BEG ");
Transaction tx = null;
Connection conn = null;
CallableStatement cs = null;
Session session = HibernateUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
conn = session.connection();
System.out.println("Connection = "+conn);
if (cs == null)
{
cs =
conn.prepareCall("{ ?=call P_TEST.FN_GETSUM(?,?) }");
}
cs.clearParameters();
cs.registerOutParameter(1,OracleTypes.INTEGER);
cs.setInt(2,1);
cs.setInt(3,2);
cs.execute();
int retInt=cs.getInt(1);
tx.commit();
}catch (Exception ex) {
logger.error("UsersActiion.getThroHibConnTest() | ERROR | " , ex);
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw ex;
}
}finally{
try {
if (cs != null) {
cs.close();
cs = null;
}
if(conn!=null)conn.close();
} catch (Exception ex){;}
}
logger.debug("UsersActiion.getThroHibConnTest() | END ");
}
I've written a program that takes care of registration and each time i try to insert multiple new users at a time with different id it gives the error message:
java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL130217122630580' defined on 'STCEPARTICIPANTS'.
here is the action passed from a button WHICH instructs the data to be saved:
private void printsavebtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String query1= "insert into STCEPARTICIPANTS values("
+speno1tf.getText()+",'"+sname1tf.getText()+"','"
+fname1tf.getText()+"','"+uni1cb.getSelectedItem()
+"')";
String query2= "insert into STCEPARTICIPANTS values("
+speno1tf.getText()+",'"+sname1tf.getText()+"','"
+fname1tf.getText()+"','"+uni1cb.getSelectedItem()
+"')";
String query3= "insert into STCEPARTICIPANTS values("
+speno1tf.getText()+",'"+sname1tf.getText()+"','"
+fname1tf.getText()+"','"+uni1cb.getSelectedItem()
+"')";
String query4= "insert into STCEPARTICIPANTS values("
+speno1tf.getText()+",'"+sname1tf.getText()+"','"
+fname1tf.getText()+"','"+uni1cb.getSelectedItem()
+"')";
String [] queryarray= {query1,query2,query3,query4};
int speno1,speno2,speno3,speno4;
String task;
if(fname1tf.getText().equals("")||sname1tf.getText().equals("")||speno1tf.getText().equals("")
||uni1cb.getSelectedItem().equals("-")|| fname2tf.getText().equals("")||sname2tf.getText().equals("")||speno2tf.getText().equals("")
||uni2cb.getSelectedItem().equals("-") || fname3tf.getText().equals("")||sname3tf.getText().equals("")||speno3tf.getText().equals("")
||uni3cb.getSelectedItem().equals("-") || fname4tf.getText().equals("")||sname4tf.getText().equals("")||speno4tf.getText().equals("")
||uni4cb.getSelectedItem().equals("-") ){
JOptionPane.showMessageDialog(rootPane, "Please enter the fields marked '*'");
}
else {
try{
speno1=Integer.parseInt(speno1tf.getText());
speno2=Integer.parseInt(speno2tf.getText());
speno3=Integer.parseInt(speno3tf.getText());
speno4=Integer.parseInt(speno4tf.getText());
int [] taskit = {speno1,speno2,speno3,speno4};
for(int count2=0;count2<taskit.length;count2++){
task= "select * from STCEPARTICIPANTS where spe_number="+taskit[count2];
DBOptions.executeNonQuery(queryarray[count2]);
if(SearchData.searchSpeno(task)==true){
JOptionPane.showMessageDialog(rootPane, "Sorry, this member is already in the database./t Please go to Profile to renew of view membership details. Thank you!");
}
}
the class SearchData is given below:
public static boolean searchSpeno(String task){
String query =task;
ResultSet rs = DBOptions.executeSQLQuery(query);
if(rs!=null)
{
try
{
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
if(colCount > 0)
{
try
{
if(rs.next() && ! rs.getString("spe_number").equals(""))
{
return true;
}
else
{
return false;
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e,"Search Error", 3);
return false;
}
}
else
{
//JOptionPane.showMessageDialog(null, "Invalid Employee ID","Search Error", 3);
return false;
}
}
catch(SQLException ex)
{
//JOptionPane.showMessageDialog(null, ex.getMessage(),"Error Occured", 2);
return false;
}
}
else
{
return false;
}
}
}
}
the class DBOptions is :
public static boolean executeNonQuery(String sqlString)
{
try
{
Statement stmt = con.createStatement();
stmt.executeUpdate(sqlString);
JOptionPane.showMessageDialog(null,"success!");
return true;
//return the number of rows affected
}
catch(SQLException e)
{
//display error message
JOptionPane.showMessageDialog(null, e.getMessage()+"\nPlease Try Again","Non Query Execution Failure", 1);
e.printStackTrace();
return false;
}
}
public static ResultSet executeSQLQuery(String sqlQuery)
{
try
{
Statement stmt = con.createStatement();
return stmt.executeQuery(sqlQuery); //query successfully executed
}
catch(SQLException e)
{
//display error message
JOptionPane.showMessageDialog(null, e.getMessage()+"\nPlease Try Again","Query Execution Failure", 1);
return null; //sql query execution failed
}
}
}
Please, i have seen some problems like this and i have tried the different forms of solution but no head way. I need to get this ready for a mini project defense. I appreciate your response. Thank you.
How did you create the table? Can you paste the CREATE TABLE statement into your question?
Each row in your table must have a unique value for each column which is defined either as PRIMARY KEY or as UNIQUE, and Derby is enforcing that constraint.
Go back to your table definition, figure out for which columns you have specified either PRIMARY KEY or UNIQUE on the column, and then look at your program to figure out what values you are providing for those columns.
Then modify your program so that it provides a unique value for each such column.