How to update ResultSet in Sqlite in Java - java

I want to update my ResultSet with using SQLite in Java. When i am trying to update the Resultset it is giving me an exception : java.sql.SQLException: not implemented by SQLite JDBC driver. i have also done :
stmt = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
but still getting the exception, i think it is because we cannot update the result Set using SQLite DB. Please help that how can i update the Sqlite Resultset in java.

If you want to get different values than those actually stored in the database, you can do these changes in the SQL query itself.
In the most general case, you can use a CASE expression:
SELECT ID,
Name,
[...],
CASE Status
WHEN 'A' THEN 'active'
WHEN 'D' THEN 'inactive'
ELSE 'whatever'
END AS Status
FROM MyTable

Related

Fetch primary key generated by sequence using jdbctemplate / jdbc for Hana db

We are trying to insert record in hana db so we are currently using jdbc template and key holder to save data and retrieve newly generated column id.This works fine for postgress but not for hana.
query ="SELECT CURRENT_IDENTITY_VALUE() FROM \"HALOSYS\".\"HaloTestDemo\"";
resultSet = stmt.executeQuery(query);
Above statement gives current identity value but this doesn't fit into our context where we want to use jdbc template.
Please give me idea to fix this.
if we add preparedstatement generated keys i am getting follwing exception
com.sap.db.jdbc.exceptions.jdbc40.SQLFeatureNotSupportedException: Method prepareStatement( String, int )() of Connection is not supported.
Try from DUMMY instead of the table name, and do it in the same session as the insert. If you use connection pooling, that will be much harder.
Please prepare your query statement to include insert record and select CURRENT_IDENTITY_VALUE() from the same table into one query statement in an anonymous block as below :
DO BEGIN
INSERT INTO <TABLE_NAME> .....;
SELECT CURRENT_IDENTITY_VALUE() FROM <TABLE_NAME>;
END;

How to use ref cursor to get multiple rows and retrieving them in Java?

The problem statement is like this :
I have to create a procedure to fetch all the data (only 9 records actually) in Employees1 table and then display that data by calling this procedure in Java. I am working on a legacy application that is built on struts 1.3 but that should not be of any concern here i guess.
Below is the procedure I created:
CREATE OR REPLACE PROCEDURE getALLEmployees(ref_cur out sys_refcursor)
IS
BEGIN
OPEN ref_cur FOR 'SELECT * FROM employees1';
END;
And here is my Java Code:
callableStatement = conn.prepareCall("{call getAllEmployees()}");
// Below line is (EmployeeDataJdbc.java:33) as mentioned in error
callableStatement.registerOutParameter(1,oracle.jdbc.driver.OracleTypes.CURSOR);
boolean isResultSet = callableStatement.execute();
rs = (ResultSet)callableStatement.getObject(1);
/*Employee paramater Order in DB
employee_id, employee_first_name, employee_last_name, employee_address,
employee_blood_group, employee_email, employee_department, employee_role,
employee_band, employee_mobile_number*/
while(rs.next()){
emp.setEmployeeId(rs.getInt(1));
emp.setEmployeeFirstName(rs.getString(2));
emp.setEmployeeLastName(rs.getString(3));
emp.setEmployeeAddress(rs.getString(4));
emp.setEmployeeBloodGroup(rs.getString(5));
emp.setEmployeeEmail(rs.getString(6));
emp.setEmployeeDepartment(rs.getString(7));
emp.setEmployeeRole(rs.getString(8));
emp.setEmployeeBand(rs.getString(9));
emp.setEmployeeMobileNumber(rs.getLong(10));
employeeList.add(emp);
}
This is the error I am getting at console:
Connected to Database
java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleCallableStatement.registerOutParameterInternal(OracleCallableStatement.java:121)
at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:268)
at oracle.jdbc.driver.OracleCallableStatement.registerOutParameter(OracleCallableStatement.java:348)
at jdbcHandles.EmployeeDataJdbc.getEmployeeList(EmployeeDataJdbc.java:33)
I have not used PL-SQL since a long long time. Please help me find out where I am going wrong. Thanks in advance.
Note: I am using Oracle 10G express edition as database.
callableStatement = conn.prepareCall("{call getAllEmployees(?)}");
You have to mention where the out parameter has to be binded using the ?
And since the column count can change. Better have a check using the Metdatadata call rSet.getColumnCount()

Update query mysql with JDBC

![enter image description here][1]I am trying to update data in table of MySQL database.
I am unable to do, since I am beginner so I have no idea how to do , kindly guide me properly.
I would be thankful to you..............
Part of cade that generates exception:
java.sql.PreparedStatement statement = conection.prepareStatement("UPDATE patient_details set `Reg_Date`='?', `Name`='?', `Father_Husband_Name`='?', `Address`='?', `City`='?', `Cell_No`='?', `Martial_Status`='?', `Gender`='?', `Status`='?', `Age`='?' where 'Reg_No'='temp'");
statement.setInt(1, temp);
statement.setString(2,textField_3.getText());
statement.setString(3,textField_1.getText());
statement.setString(4,textField_2.getText());
statement.setString(5,textArea.getText());
statement.setString(6,textField_4.getText());
statement.setString(7,textField_5.getText());
statement.setString(8,(String) comboBox.getSelectedItem());
statement.setString(9,(String) comboBox_1.getSelectedItem());
statement.setString(10,(String) comboBox_2.getSelectedItem());
statement.setInt(11,temp1);
statement.executeUpdate();
Exception is:
parameter out of range(1>number of paarameters which is 0)
YOu set 11 parameters but only have 10 ? in the statement. The last part of the statement, the where clause - you have
where 'Reg_No'='temp'"
which is not a variable. You should be able to drop the statement
statement.setInt(11,temp1);

get value from prepared statement query android?

i have a doubt that how to get the result of the prepared statement query in android.Actually i have a need that i want the row id from database while comparing a field words which can contain ' or" so i want to use prepared statement ,after googling out i did not get any proper example for android sqlite database ,please tell me how to use the prepared statement in android and after running query ,how to use value either through result set or through cursor.below is query look like-
String perfect_stmnt="select ID from Annotation where HighlightedWord=? ";
try{
** Connection con=DriverManager.getConnection("file:/"+ db.getPath());**
pstmt = con.prepareStatement(perfect_stmnt);
pstmt.setString(1, highlightword);
Resultset rs = pstmt.executeQuery();
Here the major doubt i am having how to get the connection here see the line having **
Thanks
The Android database API can prepare a statement (see compileStatement), but it is not possible to get a cursor or result set from that.
You can use compiled statements only if they return a single value, or nothing.
Please note that SQLite does not have a large overhead when preparing statements, so you can just call query or rawQuery multiple times.

SQLite Exception: Insert statement does not return a Statement

I'm using SQLiteJDBC as the wrapper for an embedded database for my small Java app. Everytime I execute an INSERT statement, I get the following exception:
query does not return ResultSet
I am wondering if the JDBC Statement.executeStatement(String) method is looking for me to return a ResultSet, but that the SQLite driver knows that INSERT statements don't return anything; maybe SQLiteJDBC is throwing an error because it shouldn't be returning the ResultSet that Statement is asking for?!?
Here is the code that is producing the exception - I have made sure to setup and close all resources properly:
statement = con.createStatement();
String sql = "INSERT INTO widgets (widget_age) VALUES (27)";
statement.executeStatement(sql);
Any ideas?!?
When you are making a change and not asking for a result back, you need to call executeUpdate() instead of executeStatement().
EDIT
I can't even find a reference to executeStatement() anywhere. Were you using executeQuery()?
Sqlite always returns a message quen you execute a Query.
It's normal to have that warning/error, it's simply that no rows where returned so you can't use them in the callback if you defined one.
PD: I also think you meant executeQuery

Categories

Resources