I need to execute this SQL code:
exec ais_disp.p_lk.p_add_client
(v_sis_id => 1640,
v_proc_id => 1,
v_time_start => to_date('01032013 00:00','ddmmyyyy hh24:mi'),
v_time_end => to_date('31032013 23:59','ddmmyyyy hh24:mi'));
select * from ais_disp.v_lk_1;
commit;
And get a ResultList from this query.
I tried to do it like this:
CallableStatement stmt = connection.prepareCall("{call ais_disp.p_lk.p_add_client " +
"(1640,
1,
to_date('01032013 00:00','ddmmyyyy hh24:mi'),
to_date('31032013 23:59','ddmmyyyy hh24:mi'))}");
stmt.execute();
ResultSet rs2 = stmt.executeQuery("select * from ais_disp.v_lk_1");
System.out.println(rs2);
while (rs2.next()){
System.out.println(rs2.getString("LRP_STATUS_NAME"));
}
stmt.close();
But it returns empty ResultSet. What I did wrong?
The database is Oracle database.
UPDATE:
I have added this string to code before stmt.executeQuery("select * from ais_disp.v_lk_1"):
stmt.execute();
But it still returns no rows.
Maybe I need to add somehow select statement in callable statement. So how to do it?
Seems like your CallableStatement has never been executed, first execute your stored procedure:
ResultSet rs = stmt.executeQuery()
Your code executes selects query instead of stored procedure, and the select statement is returning no rows.
Related
I am using Java netbeans and mysql. I want to check whether the value entered by the user in a textfield tf is already present in the mysql table or not.
String query1="SELECT * FROM trytable WHERE name='8'";
ResultSet rs=stmt.executeQuery(query1);
if(rs.isBeforeFirst()==true){JOptionPane.showMessageDialog(null,"already");}
In the above code in place of 8 I want to give the value that the user input in the form and then check whether that value already exist in form or not.
Please help me in the first line . Thanks
You should use a PreparedStatement instead of a regular statement. This is more secure than a normal Statement and allows you to avoid SQL injection issues.
You would change your query like so:
String query = "SELECT * FROM trytable WHERE name='?';";
Note the ? at the end of the query. This can be replaced later in your code when setting up the PreparedStatement:
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, userInput);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) System.out.println("Record exists!");
Here, you are telling the prepared statement to replace the first ? in the query, with the value of userInput. So, if the user inputs a 3, the query that gets executed would be SELECT * FROM trytable WHERE name=3;.
Also note that rs.next() returns true if the query returns any results, so that would be the proper way to determine if the record exists.
ResultSet is like a table, it has a cursor. At the beginning the cursor is above the first row so isBeforeFirst() will always return true even there are no results in the ResultSet.
In order to retrieve results you need to move the cursor to the next row, to do that you can use,
rs.next()
If the cursor moved to the next row successfully (which means there are more results) it will return true otherwise false. As you only need the first result you can also use,
rs.first()
to confirm there are data available in the returned ResultSet.
Try,
if (rs.first()) {
JOptionPane.showMessageDialog(null, "already");
}
This is the final code will is working absolutely fine.
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
String query = "SELECT * FROM table_name WHERE name=?;";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1,jtf.getText());
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()==true){
JOptionPane.showMessageDialog(null,"Value already exist");
}
else{
JOptionPane.showMessageDialog(null,"Value not present");
String query1="INSERT INTO table_name(col_name) VALUES (?)";
preparedStatement = conn.prepareStatement(query1);
preparedStatement.setString(1,jtf.getText());
preparedStatement.execute();
JOptionPane.showMessageDialog(null,"DONE");
}
rs.close();
preparedStatement.close();
}
catch(Exception e){
System.out.println("Exception:"+e.getMessage());
}
i have the below code, where I'm inserting records to a table. When I try to get resultset, it returns null. How to get the latest added row into a resultset?
String sql1 = "INSERT INTO [xxxx].[dbo].[xxxxxx](WORKFLOW_SEQ_NBR," +
" WORKFLOW_LOG_TYPE_CODE, WORKFLOW_STATUS_CODE, DISP_CODE, DISP_USER, DISP_COMMENT, DISP_TITLE, DISP_TS)" +
"VALUES(?,?,?,?,?,?,?,?)";
PreparedStatement pst = connect.prepareStatement(sql1);
pst.setString(1, ...);
pst.setString(2, ...);
...
...
...
pst.executeUpdate();
ResultSet rstest = pst.executeQuery();
// ResultSet rstest = pst.getResultSet();
EDIT: Resolved
added following method to go to the last added row
st.execute("Select * from [xxxx].[dbo].[xxxxxxxxx]");
ResultSet rstest = st.getResultSet();
rstest.afterLast();
GETLASTINSERTED:
while(rstest.previous()){
System.out.println(rstest.getObject(1));
break GETLASTINSERTED;//to read only the last row
}
When using a SQL statement such as INSERT, UPDATE or DELETE with a PreparedStatement, you must use executeUpdate, which will return the number of affeted rows. In this case there is simply no ResultSet produced by the sql operation and thus calling executeQuery will throw a SQLException.
If you actually need a ResultSet you must make another statement with a SELECT SQL operation.
See the javadoc for PreparedStatement#executeQuery and PreparedStatement#executeUpdate
Seems like this is an older question, but i'm looking for a similar solution, so maybe people will still need this.
If you're doing an insert statement, you can use the :
Connection.PreparedStatement(String, String[]) constructor, and assign those to a ResultSet with ps.getGeneratedKeys().
It would look something like this:
public void sqlQuery() {
PreparedStatement ps = null;
ResultSet rs = null;
Connection conn; //Assume this is a properly defined Connection
String sql = "insert whatever into whatever";
ps = conn.prepareStatement(sql, new String[]{"example"});
//do anything else you need to do with the preparedStatement
ps.execute;
rs = ps.getGeneratedKeys();
while(rs.next()){
//do whatever is needed with the ResultSet
}
ps.close();
rs.close();
}
Connection#prepareStatement() - Creates a PreparedStatement object for sending parameterized SQL statements to the database.
which means connect.prepareStatement(sql1); created the PreparedStatement object using your insert query.
and when you did pst.executeUpdate(); it will return the row count for SQL Data Manipulation Language (DML) statements or 0 for SQL statements that return nothing
Now if you again want to fetch the data inserted you need to create a new PreparedStatement object with Select query.
PreparedStatement pstmt = connect.prepareStatement("SELECT * FROM tableName");
then this shall give you the ResultSet object that contains the data produced by the query
ResultSet rstest = pstmt.executeQuery();
Which should be faster?
1) selecting thrice on an SQL server and then adding the results into array lists on Java,
SQL Server
select count(1) from table where somecriteria = "true"; -- stmt1
select count(1) from table where somecriteria = "false"; -- stmt2
select count(1) from table; -- stmt3, values are either true or false only
Java
ResultSet rs1 = stmt1.executeQuery();
while(rs1.next()){
arrayList1.add(someKeyGeneratingMethod1());
}
ResultSet rs2 = stmt2.executeQuery();
while(rs2.next()){
arrayList2.add(someKeyGeneratingMethod2());
}
ResultSet rs3 = stmt3.executeQuery();
while(rs3.next()){
arrayList3.add(someKeyGeneratingMethod3());
}
or, 2) selecting once using SQL Server and then processing the results one by one into array lists on Java?
SQL Server
select count(1) from table; -- stmt, values are either true or false only
Java
ResultSet rs = stmt.executeQuery();
while(rs.next()){
String x = rs.getString(1);
if("true".equals(x)){
arrayList1.add(someKeyGeneratingMethod1());
} else {
arrayList2.add(someKeyGeneratingMethod2());
}
arrayList3.add(someKeyGeneratingMethod3());
}
Since it is just 3 counts there is no need to execute 3 statements using java. It will slow down the process in oppose to getting it from one query and processing inside java. This is because executing an SQL statement using java has it's own workflow and you'll be invoking this workflow 3 times.
I am trying to call a stored function in Java. Function has no package, it's placed under user (schema) USER and returns a cursor. I tried twoways to call it and none of these works.
First one
Query query = coreDao.getEntityManager().createNativeQuery("{call USER.gen_rephead_sm_task_report(?, ?) }");
query.setParameter(1, dateFrom);
query.setParameter(2, dateTo);
List<?> queryResult = query.getResultList();
I got a not a procedure or not defined
I also tried this approach with
select gen_rephead_sm_task_report(?, ?) from dual
as a createNativeQuery parameter but with the same result.
Second one
Connection connection = dataSource.getConnection();//javax.sql.DataSource
CallableStatement statement = connection.prepareCall("{? = call USER.gen_rephead_sm_task_report(?, ?) }");
statement.registerOutParameter(1, OracleTypes.CURSOR);//oracle.jdbc.OracleTypes
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.executeQuery();
ResultSet set = ((OracleCallableStatement) statement).getCursor(1);
I got the ClassCastException on the last line (obviously OracleCallableStatement doesn't implement the CallableStatement). So which types shoud I use here?
Ok, so I found a link which solves my problem (iDevelopment). Here is what I used
connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall(TIMESHEET_QUERY);
statement.registerOutParameter(1, OracleTypes.CURSOR);
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.execute();
ResultSet set = (ResultSet) statement.getObject(1);
Everything is straight from javax.sql.* so there is no need for Oracle Specific API.
Im trying to write sample stored functions in postgresql and call them using the CallableStatement offered by JDBC.
Here's some my test code
Consumer bean =new Consumer();
CallableStatement pstmt = null;
try {
con.setAutoCommit(false);
String query = "{ ? = call getData( ? ) }";
pstmt = con.prepareCall(query);
pstmt.registerOutParameter(1, Types.OTHER);
pstmt.setInt(2,5);
pstmt.execute(); // execute update statement
bean=(Consumer)pstmt.getObject(1);
System.out.println("bean"+bean.getConsumer_name());
And my Stored function is of the form .
CREATE FUNCTION getData(int) RETURNS SETOF db_consumer AS $$
SELECT * FROM db_consumer WHERE consumer_id = $1;
$$ LANGUAGE SQL;
However, I'm getting the following error when I try to run the code .
org.postgresql.util.PSQLException: A CallableStatement was executed with an invalid number of parameters .
Any idea why this could be happening?
I don't think you need a CallableStatement as you should be able to run select * from getData(5) directly:
PreparedStatement pstmt = con.prepareStatement("select * from getData(?)")
pstmt.setInt(1,5);
ResultSet rs = pstmt.execute();
while (rs.next()) {
System.out.println(rs.getString(1));
}
You are trying to call a SETOFF function via a Callable Statement. That's not going to happen! You'll always get an error.
PostgreSQL's stored functions can return results in two different ways. The function may return either a refcursor value or a SETOF some datatype. Depending on which of these return methods are used determines how the function should be called.
Functions that return data as a set should not be called via the CallableStatement interface, but instead should use the normal Statement or PreparedStatement interfaces.