Query to delete from multiple tables in Access - java

I want to delete one row from multiple tables in an Access database.
This is the code I tried to use in my project, but I got an error.
PreparedStatement ps = con.prepareStatement("DELETE FROM 'customer_details' , 'papers', 'magzines' WHERE 'customer_id' = ? ");
ps.setString(1,tx1.getText());
int string = ps.executeUpdate();
Can anyone help me solve this?

I just tried the following and it worked for me:
String sql =
"DELETE t1.*, t2.*, t3.* " +
"FROM " +
"(" +
"Table1 AS t1 " +
"INNER JOIN " +
"Table2 AS t2 " +
"ON t2.ID=t1.ID " +
")" +
"INNER JOIN " +
"Table3 AS t3 " +
"ON t3.ID=t2.ID " +
"WHERE t1.ID=?";
ps = con.prepareStatement(sql);
ps.setInt(1, 4); // delete where ID=4
int n = ps.executeUpdate();
So in your case try something like this:
PreparedStatement ps = con.prepareStatement(
"DELETE c.*, p.*, m.* " +
"FROM " +
"(" +
"customer_details AS c " +
"INNER JOIN " +
"papers AS p " +
"ON p.customer_id=c.customer_id " +
")" +
"INNER JOIN " +
"magzines AS m " +
"ON m.customer_id=p.customer_id " +
"WHERE c.customer_id=?");
ps.setString(1,tx1.getText());
int n = ps.executeUpdate();

Related

Hibernate Error: could not extract ResultSet

I have this error in my code:
"Request processing failed; nested exception is javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet] con causa raĆ­z
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-206,
SQLSTATE=42703, SQLERRMC=INVOICESEL2_.CODE_K, DRIVER=4.14.88"
I am not able to find the error, I know that the SQL code -206 refers to An object is specified in a context where it is not valid.
My code is this:
public InvoiceSeller getSellerByEmail(String email, String debtorCode) {
String qString = "SELECT d FROM " + InvoiceSeller.class.getSimpleName() + " d "
+ " INNER JOIN "+ InvoiceSellerContact.class.getSimpleName() + " sc "
+ " ON d.code = sc.invoiceSeller.code AND d.invoiceDebtor.code = sc.invoiceSeller.invoiceDebtor.code "
+ " INNER JOIN "+ Contact.class.getSimpleName() + " c ON sc.contact.id = c.id "
+ " INNER JOIN " + ActiveRegisterMaster.class.getSimpleName() + " a "
+ " ON d.code = a.code AND d.invoiceDebtor.code = a.invoiceDebtor "
+ " WHERE a.tableName = :tablename AND d.invoiceDebtor.code = :debtorCode "
+ " AND c.email = :email AND d.status = :status";
TypedQuery<InvoiceSeller> query = this.em.createQuery(qString, InvoiceSeller.class);
System.out.println(qString);
System.out.println(query.getFirstResult());
query.setParameter("debtorCode", debtorCode);
query.setParameter("email", email);
query.setParameter("tablename", InvoiceSeller.TABLENAME);
query.setParameter("status", "A");
InvoiceSeller seller = query.getSingleResult();
System.out.println(seller.toString());
return seller;
}
Thanks!
Your HQL is wrong, remember that HQL sintax is different from SQL and when you call this.em.createQuery you are trying to create a HQL.
So, your HQL should be something like this:
String qString = "SELECT d FROM " + InvoiceSeller.class.getSimpleName() + " d "
+ " INNER JOIN "+ InvoiceSellerContact.class.getSimpleName() + " sc "
+ " INNER JOIN "+ Contact.class.getSimpleName() + " c "
+ " INNER JOIN " + ActiveRegisterMaster.class.getSimpleName() + " a "
+ " WHERE a.tableName = :tablename AND d.invoiceDebtor.code = :debtorCode "
+ " AND c.email = :email AND d.status = :status";
+ " AND c.email = :email AND d.status = :status";
Note that i removed the ON keywords after INNER JOIN, beacuse HQL don't need it.

HSQLDB: Encountered a duplicated sql alias

When I execute this query using a self-join from my java program
Query query = session.createSQLQuery("SELECT DISTINCT * " +
"FROM lerneinheit AS le1 JOIN lerneinheit AS le2 " +
"ON le1.datum = le2.datum AND le1.pid = le2.pid " +
" WHERE " +
" le1.datum BETWEEN '2016-10-20' AND '2016-10-20' AND " +
" le1.pid = 3 AND " +
" (le1.abgesagtrechtzeitig = false OR le1.nichtabgesagt = true OR le1.erschienen=true) AND " +
" (le2.abgesagtrechtzeitig = false OR le2.nichtabgesagt = true OR le2.erschienen=true) AND " +
" le1.lernid!= le2.lernid AND " +
" (le2.beginn+1 BETWEEN le1.beginn AND le1.ende OR le2.ende-1 BETWEEN le1.beginn AND le1.ende) " +
" ORDER BY le1.beginn");
I get the following error:
org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [LERNID] during auto-discovery of a native-sql query
Although it works fine if I do this from phpAdmin. Everything I found on that topic wasn't helpful at all. Anyone got any idea how to solve that?
It may occurred when there are two or more column/alias exists with the SAME NAME in your select query.
Like...
Select A.NAME, A.ADDRESS, B.NAME, B.AGE from TABLE_A A join TABLE_B B on A.SOME_ID = B.SOME_ID;
Encountered a duplicated sql alias [NAME] during auto-discovery of a native-sql query will show.
I'm not sure but the HQL equivalent for != is <>, so you should write "le1.lernid <> le2.lernid AND"
And by the way I recommend :
Query query = session.createSQLQuery("SELECT DISTINCT * " +
"FROM lerneinheit AS le1 JOIN lerneinheit AS le2 " +
"ON le1.datum = le2.datum AND le1.pid = le2.pid " +
" WHERE " +
" le1.datum BETWEEN :dateMin AND :dateMax AND " +
" le1.pid = :le1Pid AND " +
" (le1.abgesagtrechtzeitig = false OR le1.nichtabgesagt = true OR le1.erschienen = true) AND " +
" (le2.abgesagtrechtzeitig = false OR le2.nichtabgesagt = true OR le2.erschienen = true) AND " +
" le1.lernid != le2.lernid AND " +
" (le2.beginn + 1 BETWEEN le1.beginn AND le1.ende OR le2.ende - 1 BETWEEN le1.beginn AND le1.ende) " +
" ORDER BY le1.beginn");
query.setParametter("dateMin", "2016-10-20");
query.setParametter("dateMax", "2016-10-20");
query.setParametter("le1Pid", 3);

Can't figure out why this big double inner join isn't working, but when split it does

Here is my queries split up that work perfectly fine...
String sqlstatement = "SELECT WBLinkWebsiteID, WBLinkCategoryParentID, WBLinkTitle, WBLinkURL FROM WEBSITECATEGORY_TABLE WHERE WBLinkCategoryID = ?";
String[] args = { CategorySubID };
Part 2
sqlstatement = "SELECT LOCATIONS_TABLE.LocationWebsiteID, "
+ "LOCATIONS_TABLE.locationCity, "
+ "LOCATIONS_TABLE.locationState, "
+ "LOCATIONS_TABLE.locationCountry, LOCATIONS_TABLE.locationType, "
+ "LOCATIONS_TABLE.locationUrl, "
+ "PREF_TABLE.Pref_SavedTitle "
+ "FROM PREF_TABLE INNER JOIN "
+ "LOCATIONS_TABLE ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID "
+ "WHERE "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "' ORDER BY LOCATIONS_TABLE.locationState, LOCATIONS_TABLE.locationCity";
Now here is my attempt to combine the two instead of just having 2 go in row back to back and burn time/resources...
String NewSqlstatement = "SELECT LOCATIONS_TABLE.LocationWebsiteID, "
+ "LOCATIONS_TABLE.locationCity, "
+ "LOCATIONS_TABLE.locationState, "
+ "LOCATIONS_TABLE.locationCountry, "
+ "LOCATIONS_TABLE.locationUrl, "
+ "LOCATIONS_TABLE.LocationID, "
+ "PREF_TABLE.Pref_SavedTitle, "
+ "WEBSITECATEGORY_TABLE.WBLinkTitle, "
+ "WEBSITECATEGORY_TABLE.WBLinkURL "
+ "FROM PREF_TABLE INNER JOIN "
+ "LOCATIONS_TABLE ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID "
+ "INNER JOIN WEBSITECATEGORY_TABLE "
+ "ON WEBSITECATEGORY_TABLE.WBLinkWebsiteID = PREF_TABLE.Pref_WebsiteID "
+ "WHERE "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "'";
Now when I try to do my "SINGLE" way it keeps returning the WHOLE database of Locations in the LOCATIONS_TABLE query. It doesn't do just the exact ones I need.
I know the query works, because I have tested it here: http://sqlfiddle.com/#!6/ede97/2
Now I know my example on sqlfiddle is using MS Server 2014, but I assumed the syntax should be pretty much the same since its just standard SELECT with inner joins, but I could be wrong?
Anyone know what I'm doing wrong? Any help is greatly appreciated
EDIT - Fixed the SQLFIDDLE, I put the wrong statement in the example
Aren't you missing your filter on WBLinkCategoryID in the combined query. Shouldn't you have this:
...
+ "INNER JOIN WEBSITECATEGORY_TABLE "
+ "ON WEBSITECATEGORY_TABLE.WBLinkWebsiteID = PREF_TABLE.Pref_WebsiteID "
+ "WHERE "
+ "WEBSITECATEGORY_TABLE.WBLinkCategoryID IN (<value1>,...,<valueN>) AND "
+ "PREF_TABLE.Pref_SavedTitle = '" + theSavedPref + "'";

Java sql populating table and getting '.0' at the end

im using this code to populate my jtable with data from database:
String sql = "SELECT "
+ "a.pranesimo_pav AS 'Pavadinimas', a.sukurimo_data AS 'Sukurtas' , b.gavimo_data AS 'Gavimo data'"
+ "FROM Pranesimas a "
+ "INNER JOIN "
+ "( "
+ "SELECT pranesimo_id, gavimo_data "
+ "FROM Pranesimo_siuntimas_vienam_zmogui "
+ "WHERE vardas_pavarde = '" +vardas_pavarde+ "' "
+ "UNION ALL "
+ "SELECT pranesimo_id, gavimo_data "
+ "FROM Pranesimo_siuntimas_grupei "
+ "WHERE grupes_pav = '" +grupes_pav+ "'"
+ ")b ON a.pranesimo_id = b.pranesimo_id ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
antraspanelesamipranesimaiTable.setModel(DbUtils.resultSetToTableModel(rs));
}
It does the job, but at the end of my 'datetime' value, it adds '.0' at the end of it, for example:
value in database - '2013-01-20 02:50:00'
value i get in my table - '2013-01-20 02:50:00.0'
how can i fix this?

Sql update query with Single quotes

I am using hibernate application in java to retrieve and update database.
During updating a table,i forming an sql query as follows,
String qry = "UPDATE " + entity + " SET " + htmlColumn + " ='"+value+"' WHERE " + id + " = " + primaryId;
where value is a html string which contains single quotes sometimes.
How to escape ignore/escape the single quotes and update the table successfully
Thanks
use PreparedStatement for this
String qry = "UPDATE " + entity +
" SET " + htmlColumn + " = ? " +
"WHERE " + id + " = ?";
PreparedStatement pstmt = con.prepareStatement(qry);
pstmt.setString(1, value);
pstmt.setInt(2, primaryId);
pstmt.executeUpdate();
PreparedStatement
Don't set values directly.
currentSession()
.createQuery("UPDATE " + entity + " SET " + htmlColumn +
" = :value WHERE " + id + " :id")
.setParameter("value", value).setParameter(":id",id).executeUpdate();
You can replace the single quote with a double single quote. value.replace("'","''"); but you will need to cater for more than just that because your value can easily allow for SQL Injection if it is not properly catered for.
You can use preparedstatement as :
String query= "UPDATE " + entity + " SET " + htmlColumn + " =? WHERE " + id + " = " + primaryId;
PreparedStatement ptmt = con.prepareStatement(query);
ptmt.setString(1, value);

Categories

Resources