I need to execute this SQL code:
exec procedure(param, param, param, param)
select * from bla_bla_table;
commit;
And get a ResultList from this query.
I tried to do it like this:
CallableStatement stmt = connection.prepareCall("{call procedure(param,param,param,param)}");
stmt.execute();
How can I insert sql statement select * from bla_bla_table; here before commit to get the ResultSet. I tried a lot of ways to do that... but nothing helps.
Did you try this?
connection.setAutoCommit(false); // Disable Auto Commit
CallableStatement stmt = connection.prepareCall("{call procedure(param,param,param,param)}");
stmt.execute();
Statement stmt2 = connection.createStatement();
ResultSet rs = stmt2.executeQuery("select * from bla_bla_table"); // Result set for Query
connection.commit();
Add this code after the execution of your code.
PreparedStatement prep = connection.prepareStatement(string);
ResultSet rs = prep.executeQuery();
// now iterate the resultset.
Before everything you should make sure that you run a transaction by setting the autocommit option of the connection to false.
connection.setAutoCommit(false);
Related
I called a postgresql function with Callable in JDBC, but it show exception
"function does not exis"
con = dataSource.getConnection();
con.setAutoCommit(false);
CallableStatement stmt = con.prepareCall("{call myfunction(?)}");
stmt.registerOutParameter(1, Types.REF_CURSOR);
stmt.execute();
con.commit();
However, when i add
stmt.setObject(1, null);
before registerOutParameter , it works.
I can not understand what happen
I am trying to move all query executions from Statement to PreparedStatement due to SQL injection. My original issue was with update statement, but I wanted to try it with select statement as well. When I execute the below line of code, the statement returns nothing.
String selectQuery = "select is_enabled, syllabus_id from ic_syllabus where syllabus_id=?";
PreparedStatement pstmt = conn.prepareStatement(selectQuery);
pstmt.setString(1, "25AC1CFB7C1A2CF07F176BD3A296F229");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String flag = rs.getString(1);
String sybsId = rs.getString(2);
}
I am using Oracle database and am not getting any exceptions either.
When I debug, I get this error :
Column 'place1' not found.
I was able to verify that it has column place1 in sql.
Is it because I can not have two database connection in one function? I am unsure on how to further debug the problem.
Case.java
System.out.println("The highest value is "+highest+"");
System.out.println("It is found at index "+highestIndex+""); // until now it works fine
String sql ="Select Day from menu where ID =?";
DatabaseConnection db = new DatabaseConnection();
Connection conn =db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, highestIndex);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
int kb=rs.getInt("Day");
System.out.println(kb);
if(kb==k) // k is a value getting from comboBox
{
String sql1 ="Select * from placeseen where ID =?";
DatabaseConnection db1 = new DatabaseConnection();
Connection conn1 =db1.getConnection();
PreparedStatement ps1 = conn.prepareStatement(sql);
ps.setInt(1, highestIndex);
ResultSet rs1 = ps.executeQuery();
if (rs1.next())
{
String aaa=rs1.getString("place1");
String bbb=rs1.getString("place2");
Tourism to =new Tourism();
to.setPlace1(aaa);
to.setPlace2(bbb);
DispDay dc=new DispDay();
}
ps1.close();
rs1.close();
conn1.close();
}
else
{
System.out.print("N");
System.out.println("Sorry!!!");
}
}
ps.close();
rs.close();
conn.close();
Trace your code to see where you're getting the data. The error is on this line:
String aaa=rs1.getString("place1");
Where does rs1 come from?:
ResultSet rs1 = ps.executeQuery();
Where does ps come from?:
PreparedStatement ps = conn.prepareStatement(sql);
Where does sql come from?:
String sql ="Select Day from menu where ID =?";
There's no column being selected called place1. This query is only selecting a single column called Day.
Maybe you meant to get the result from the second prepared statement?:
ResultSet rs1 = ps1.executeQuery();
There are probably more such errors. Perhaps several (or many) more. Because...
Hint: Using meaningful variable names will make your code a lot easier to follow. ps, ps1, rs1, etc. are very easy to confuse. Name variables by the things they conceptually represent and your code starts to read like a story which can be followed. Variable names like daysQuery and daysResults and placesResults make it more obvious that something is wrong when you try to find a "place" in a variable which represents "days".
In your second query:
PreparedStatement ps1 = conn.prepareStatement(sql);
you are accidentally using the variable sql instead of your previously defined sql1. Replace it and it will be ok.
Following is the code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:cse");
//Statement stmt;
ResultSet rset;
//stmt = conn.createStatement();
String sql = " Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(sql);
while (rset.next()) {
arr.add(rset.getInt("id"));
arr.add(rset.getString("first"));
arr.add(rset.getString("last"));
arr.add(rset.getInt("age"));
}
System.out.println(arr);
pst.close();
conn.close();
For the above am getting "Error: java.sql.SQLException: Driver does not support this function". What might be the problem?
You are misusing the PreparedStatement interface. When using PreparedStatements, you should prepare the statement with your query, bind all necessary parameters and then execute it without any SQL - this will cause the statement to execute the previously prepared SQL statement:
String sql = "Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(); // Note - No args in the executeQuery call
Is there any way to call SQL Function during updateXXX method in ResultSet of type ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE.
For example look at this code fragment:
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(query);
while (rs.next()) {
rs.updateTimestamp("status_time", "NOW()"); // call some SQL function during update
rs.updateRow();
}
Is there any way to update value such way? The above example ofcourse don't work.
You can't do that. These methods are for setting actual values, not for executing SQL functions.