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

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?

Related

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 + "'";

SQLite How To - Sum by Value in a JOIN

I have a sample sqlite database that currently has 7 entries which is queried via:
String selectQuery = "SELECT * FROM " + TABLE_TRANSACTIONS + " x " +
"JOIN (SELECT " + KEY_TYPE_ID + ", COUNT(*) count FROM " + TABLE_TRANSACTIONS + " GROUP BY " + KEY_TYPE_ID + " ORDER BY count DESC) y " +
"ON x.type_id = y.type_id " + //type_id vs. KEY_TYPE_ID as alias won't recognize KEY_TYPE_ID
"WHERE x.type_id = " + TagData.TYPE_EXPENSE;
The output is:
Type A = 2500
Type A = 2599
Type B = 45000
Type C = 299
Type C = 2699
Type C = 10000
Type C = 12000
which correctly groups my types by their respective values. However, the ideal output would be:
Type B = 45000
Type C = 24998
Type A = 5099
where each type is then ordered by the sum of each type. Is this possible? If so what else should I be doing in my query? I'm relatively new to SQL and haven't been able to figure this out yet. Thank you in advance for any insight.
EDIT
Based on your input #CL. I now have a more simplified query:
String selectQuery = "SELECT *, SUM(" + KEY_AMOUNT + ") AS amount_sum " +
"FROM " + TABLE_TRANSACTIONS + " " +
"GROUP BY " + KEY_LABEL_ID + " " +
"ORDER BY amount_sum DESC";
which works as expected when I use sqlfiddle at http://www.sqlfiddle.com/#!5/b3615/1 but not when I use the query in Android. In Android, only the most recent entry for each label type is returned. The SUM doesn't seem to actually do its job.
What am I missing here?
Moving the aggregation and ordering into a subquery does not make sense.
If you want to get the count of all expense transactions per type, just use a simple aggregation:
SELECT Type_ID,
COUNT(*) count
FROM TRANSACTIONS
WHERE TYPE_IF = 'TYPE_EXPENSE'
GROUP BY TYPE_ID
ORDER BY count DESC
This is an aggregation query. Fortunately, SQLite supports CTEs, so you can just do something like this:
with t as (
<your query here>
)
select type, sum(value) as sumv
from t
group by type
order by sumv desc;
Even without the with clause, you could just use your query as a subquery.
You would, of course, use the appropriate column names in the query.
#CL. Your response led me down the correct path, for those interested here is the resulting query that achieved what I was after:
String selectQuery = "SELECT " + KEY_LABEL_ID + ", " + " SUM(" + KEY_AMOUNT + ") as total, " + KEY_TYPE_ID + " " +
"FROM (" +
"SELECT " + KEY_LABEL_ID + ", " + KEY_AMOUNT + ", " + KEY_TYPE_ID + " " +
"FROM " + TABLE_TRANSACTIONS + " " +
") as trans_table " +
"WHERE " + KEY_TYPE_ID + " = " + TagData.TYPE_EXPENSE + " " +
"GROUP BY " + KEY_LABEL_ID + " " +
"ORDER BY total DESC";

Query to delete from multiple tables in Access

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();

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