Update query mysql with JDBC - java

![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);

Related

How to update ResultSet in Sqlite in 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

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.

Java - MySQL --> SQLite - unable to insert in SQLite database using java

iv created a program which works really well with MySQL. However, when I convert it to SQLlite, everything works such as Creating tables, getConnection() to the database and such EXCEPT inserting values to the table??
I am unable to insert value to the table using Java (Netbeans) to SQLite database. I can only insert ONE row but not multiple rows? In MySQL, i could insert multiple rows at once, however, I cant in SQLite?
The code is (This works for only ONE row):
Connection con;
Statement s= con.CreateStatement();
String st = "Insert into table1 (10,'abc')";
s.executeUpdate(st);
s.close();
If I do something like this (DOES NOT work for more than one row - have no idea why- SQLite):
Connection con;
Statement s= con.CreateStatement();
String st = "Insert into table1 (10,'abc'), (5,'vfvdv')"; //now it doesnt work since Im inserting more than one row. I can't figure out what Im doing wrong -
//Iv also tried Insert into table1(ID, Name) values (10,'abc'), (5,'afb'); but it dont work.
s.executeUpdate(st);
s.close();
Could any java expert or anyone help me on this. I cant figure out what Im doing wrong or anything because when I type my commands in the 'SQLite' command line it works fine for ALL. But, In JAVA with SQLite, I can only update one row for some reason?
However, Java with MySQL works fine but not SQLlite.
Anyone could clarify what Im doing wrong would be brillant.
Thanks alot for reading this and your time.
It's possible that the SQLite JDBC driver doesn't support the multi-insert syntax. This syntax is also not standard, though many databases do support it.
Another option for doing multiple inserts like this is to use the batching mechanism of PreparedStatement.
Connection conn = ....;
PreparedStatement stmt =
conn.prepareStatement("INSERT INTO table1(id, name) VALUES (?, ?)");
stmt.setInt(1, 10);
stmt.setString(2, "abc");
stmt.addBatch();
stmt.setInt(1, 5);
stmt.setString(2, "vfvdv");
stmt.addBatch();
stmt.executeBatch();
Typically, you'd use the setInt, setString, addBatch sequence in a loop, instead of unrolled as I've shown here, and call executeBatch after the loop. This will work with all JDBC compliant databases.
Multi row insert is not standard according to the SQL92 standard.
SQLite suports it since version 3.7.11.
If you have a version under this one, you need to make one insert for each row...

DatabaseMetaData.getColumns() giving error with SQL Server 2008

We have a database code in place for a long time which is running successfully with Progress DB. Recently, we tried it with SQL Server 2008 with JDBC 4 driver .It gave below exception:
Database '%' does not exist. Make sure that the name is entered correctly.
DatabaseMetaData conMD = connection.getMetaData();
ResultSet columns = conMD.getColumns("%", "%", m_Table, "%");
Could anybody please help me out ?
If you get the list of catalogs before?
With the method getCatalogs()
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/DatabaseMetaData.html#getCatalogs()
As the error message says: you cannot pass a wildcard for the database (catalog) parameter. Wildcards are only allowed for the schema, table and column names.
Use null instead.
conMD.getColumns(null, "%", m_Table, "%");
conMD.getColumns(connnection.getCatalog(), "%", m_Table, "%");
instead of giving % ,connnection.getCatalog()will fix the issue.Interesting thing is that the code works with Oracle,MySql and Progress . Only Sql Server is throwing the error.

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