Why will Ucanaccess not delete my records? - java

I can connect to my access database and select, insert records etc. I am now trying to delete records and as far as I can see I am using the correct syntax. I have followed just about every tutorial I can find and they are not doing anything different that I can see.
String deleteSql = "DELETE FROM table1 WHERE sometext=? or sometext=?";
ps = module.getSupportConnection().prepareStatement(deleteSql);
ps.setString(1,"four");
ps.setString(2,"five");
int rs = ps.executeUpdate();
System.out.println(rs);
I have tried it without using int rs = .. but I used it just to see what the output was and it returns '2' which is what I was expecting as there are two records that meet the criteria used. It just wont delete the records and I cant see why. I dont get any errors when running the code. I appreciate this may not be a ucanaccess issue per se.

Related

Java prepared statement does not allow large strings in the where clause of the query

I am trying to retrieve data from DB2 using Java prepared statement
String select_statement = "SELECT * FROM schema_name.table_name where NME='xxx002' and LINE =7200 and FILE_NME='720001042021XYZ002' with ur";
try (Connection connection = DataBaseConnection.getGeoCarDBConnection_TESTDATA();
PreparedStatement ps = connection.prepareStatement(select_statement);) {
ResultSet rs = null;
rs = ps.executeQuery();
}
The problem I am facing is that I include the FILE_NME in the where clause of the query, as shown above, 0 rows are returned. But any other string fields can be passed and I get the desired number of rows.
Any integer fields in the where clause works too.
But only the string fields that are large(In this case, FILE_NME field) are not working. In the DB2 table, where I am pulling the data from, the FILE_NME field is of varchar(30).
Things that did not work for me was
String select_statement = "SELECT * FROM schema_name.table_name where NME='xxx002' and LINE =7200 and FILE_NME = ? with ur";
then I set the String value using,
ps.setString(1, "'720001042021XYZ002'")
ps.setString(1, "720001042021XYZ002")
Both did not work.
None of the google links were helpful. Have spent more than a day on it.
This code used to work flawlessly before, Even the java version hasn't changed(as per my knowledge)
I am running it in windows 10.
Java version : 1.8 ((build 1.8.0_221-b11))
I run the same query in the database client and it works.
Someone please help me or point me in the right direction. I don't know what I am missing
Thank in advance
The problem was that there was indeed no data, I was checking the same query in the database client in a different environment. I am closing this.

Slow queries with preparedStatement but not with executeQuery

I'm having a weird problem with an Grails application accessing data. Going deeper I've isolated the problem to a plain java8 small application using PreparedStatement.executeQuery vs Statement.executeQuery.
Consider the following snippet of code:
// executes in milliseconds
directSql = "select top(10) * from vdocuments where codcli = 'CCCC' and serial = 'SSSS' ORDER BY otherField DESC;";
stmt = con.createStatement();
rs = stmt.executeQuery(directSql);
// More than 10 minutes
sqlPrepared = "select top(10) * from vdocuments where codCli = ? and serial = ? ORDER BY otherField DESC;";
PreparedStatement pStatement = con.prepareStatement( sqlPrepared );
pStatement.setString(1, "CCCC");
pStatement.setString(2, "SSSS");
rsPrepared = pStatement.executeQuery();
Same query.
Data comes from a view on SqlServer (2008, I think, have no access right now) from a table with more than 15 Million records. There are indexes for all needed fields and the same query (the first one) executed from console runs also quite fast.
If I execute the slow PreparedStatement query without the ORDER clause it also runs fast.
It looks clear to me that for any cause the database it's not using indexes and make a full scan when using preparedStatement, but maybe I'm wrong so I'm open to any idea.
I thought maybe the driver (sqlserver official latest and jtds has been tested) was holding the data waiting for any kind of EOF from connection but I've checked with tcpdump on my side and no data is received.
I can't find why this is happening so any idea will be welcomed.
Thank you in advanced!
I've finally found a solution, at least in for my case. I got it here http://mehmoodbluffs.blogspot.com.es/2015/03/hibernate-queries-are-slow-sql-servers.html . Telling (driver? sqlServer?) not to send parameters as Unicode have resolved the problem.
Current connection string it's now:
String connectionUrl = "jdbc:sqlserver://server:port;databaseName=myDataBase;sendStringParametersAsUnicode=false";
And now both direct queries and preparedStatements runs at millisecond speed.
Thank you #DanGuzman for your suggestions!

Fetching records from MySQL using LIKE and %

I have table called mpi which contains 23 columns. I have introduced the search field with button for every column where user can enter the query to fetch the records using query
query="select * from mpi where Genus ='"+genus+"'
Now I want to fetch records by giving keywords using LIKE %% but it is not working and not giving any records but if type type the full name it is working perfectly. Here is the code
String uname=request.getParameter("uname");
String full="%"+uname+"%";
dbconn=new DatabaseConnection();
conn=dbconn.setConnection();
pstmt=conn.prepareStatement("select * from mpi where Genus LIKE ?");
pstmt.setString(1, full);
res=pstmt.executeQuery
Could any one tell me where is the mistake and why I am not getting the records when I use half keyword like %keyword%.
It works (apart from the missing parentheses) and the approach with a prepared statement is entirely correct.
However I have seen a couple of code pieces like that, and always the problem lay with variables mix-up or not closing, or simple oversight. Better declare as close as possible.
try (ResultSet res = pstmt.executeQuery()) {
while (res.next()) {
..
}
} // Automatically closes res.
Also handle the life-cycle of pstmt correctly, with closing.

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...

Update existing database entry in java App

I am currently trying to make an update application (Java based) that the user can go through and view the existing database entries (MySQL) and edit them if need be... I was wondering how to get the information for a specific entry (ie 12-1589 which is an example of what the ID or primary key would be) and fill in the text boxes with all of the information from said entry.... I may just need to walk away from the computer for a bit because i may be over-thinking it, but I don't know...
mainly i am unsure with the exact code that you would use to move to that entry and retrieve the data from just that entry.... I know how to step trough a database one entry at a time, but i would rather just jump to a specific row based off of an id number (such as above 12-1589) if at all possible....
I just tried this and i recieved an error.... The error was:
"Unknown column '12-1859' in 'where clause'"
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement();
String sql = "SELECT * FROM Load_Sheet WHERE Load_Number = 12-1859 limit 1";
rs = stmt.executeQuery(sql);
String BC = rs.getString("BC");
If anyone could give me a hand with that is going wrong i would appreciate it...
I just started getting another error along with the other one... it is :
"illegal operation on empty result set"
Though the result sets are not empty so my guess is, is that i am missing a step somewhere....
What you need is a simple WHERE statement if i understood correctly your question.
SELECT * FROM table_name WHERE entry_col = "12-1589" LIMIT 1
LIMIT 1 is only added so that the MySql query only returns a single row.

Categories

Resources