intermittent "java.sql.SQLException: org.apache.commons.dbcp.DelegatingStatement is closed." - java

I had been facing this exception from very long time.
My tomcat/java web application having very large no of database transaction.
I am using MSSQL server as my back end and using DBCP, database connection pooling in tomcat.
I have following practice of using this.
DB pooling in my servlet.
//Skeleton
dopost
{
//geting connection using JNDI
//Creating statement object
// calling method of execution of Business Logic
}
finally
{
//closing all DB resorces
}
Business Logic
{
//do all Database transaction
}
//Original code spinet
public class Frm_mst_operation_II extends HttpServlet
{
protected void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException
{
try
{
String option=httpservletrequest.getParameter("option");
InitialContext initialcontext = new InitialContext();
if(initialcontext == null)
throw new Exception("Boom - No Context");
InitialContext initialcontext2 = new InitialContext();
if(initialcontext2 == null)
throw new Exception("Boom - No Context");
Context context = (Context)initialcontext.lookup("java:/comp/env");
Context context2 = (Context)initialcontext.lookup("java:/comp/env");
DataSource datasource = (DataSource)context.lookup(RuntimeConf.abi_database);
DataSource datasource_sql = (DataSource)context2.lookup(RuntimeConf.abi_sql_database);
if(datasource != null)
{
if(dbcon == null)
{
dbcon = datasource.getConnection();
}
else
{
if(!dbcon.isClosed())
{
dbcon.close();
}
dbcon = datasource.getConnection();
}
if(dbcon_sql == null)
{
dbcon_sql = datasource_sql.getConnection();
}
else
{
if(!dbcon_sql.isClosed())
{
dbcon_sql.close();
}
dbcon_sql = datasource_sql.getConnection();
}
if(dbcon_sql != null)
{
dbcon.setAutoCommit(false);
dbcon_sql.setAutoCommit(false);
stmt_ora1 = dbcon.createStatement();
stmt = dbcon_sql.createStatement();
if(option.equals("Insert_cge_Data"))
{
Insert_CGE_Data(httpservletrequest);
}
dbcon.close();
dbcon_sql.close();
dbcon=null;
dbcon_sql=null;
}
}
else
{
System.out.println("Data Source Not Found - Error : Exception In Frm_mst_operation_II");
}
httpservletresponse.sendRedirect(url);
}
catch(Exception exception)
{
System.out.println("Exception In Frm_mst_operation_II And Exception Is :: "+exception);
exception.printStackTrace();
}
finally
{
if(rset != null)
{
try
{
rset.close();
}
catch(SQLException e)
{
System.out.println("Exception in Frm_mst_operation_II "+e);
}
rset = null;
}
if(stmt != null)
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.out.println("Exception in Frm_mst_operation_II "+e);
}
stmt = null;
}
if(stmt_ora1 != null)
{
try
{
stmt_ora1.close();
}
catch(SQLException e)
{
System.out.println("Exception in Frm_mst_operation_II "+e);
}
stmt_ora1 = null;
}
if(dbcon != null)
{
try
{
dbcon.close();
}
catch(SQLException e)
{
System.out.println("Exception in Frm_mst_operation_II "+e);
}
dbcon = null;
}
if(dbcon_sql != null)
{
try
{
dbcon_sql.close();
}
catch(SQLException e)
{
System.out.println("Exception in Frm_mst_operation_II "+e);
}
dbcon_sql = null;
}
}
}//End Of doPost() Method ...
public void Insert_CGE_Data(HttpServletRequest request) throws IOException,
ServletException, SQLException {
try
{
query_th_ora = " INSERT INTO PR_OPR_TH " + " ( "
+ " COMP_N, YR_N, TRN_N, TRN_C, "
+ " TRN_D, MCH_N, SHIFT_N, STRT_TIME, "
+ " END_TIME, PROD_N, BODY_N, "
+ " CAR_N , FIRE_N, CREATED_UID," + " CREATED_D , " +
// " UPDATED_UID, UPDATED_D" +
" TRNTYP , TOT_QTY " + " , RSN_N ) " + " VALUES " + " ( "
+ " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
+ "', 'CGE" + TRN_C + "', " + " to_date('" + trn_d
+ "', 'dd/mm/yyyy') , '" + mch_nm
+ "', '" + shift + "', to_date('" + in_time
+ "', 'dd/mm/yyyy hh:mi:ss AM'), " + " to_date('"
+ out_time + "', 'dd/mm/yyyy hh:mi:ss AM'), '" + prod_n
+ "', '" + body_n + "', " + " '" + car_n + "', '"
+ fire_n + "', '" + user_cd + "',to_date('"+ora_sysdt+"', 'dd/mm/yyyy HH24:MI:SS'), " + " 'CGE', '" + tot_qty + "'"
+ " , '"+RSN_N+"' ) " + " ";
//SQL MD20230302
query_th_sql = " INSERT INTO PROD.PR_OPR_TH " + " ( "
+ " COMP_N, YR_N, TRN_N, TRN_C, "
+ " TRN_D, MCH_N, SHIFT_N, STRT_TIME, "
+ " END_TIME, PROD_N, BODY_N, "
+ " CAR_N , FIRE_N, CREATED_UID," + " CREATED_D , " +
// " UPDATED_UID, UPDATED_D" +
" TRNTYP , TOT_QTY " + " , RSN_N, FLAG) " + " VALUES " + " ( "
+ " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
+ "', 'CGE" + TRN_C + "', " + " PROD.to_date('" + trn_d
+ "', 'dd/mm/yyyy') , '" + mch_nm
+ "', '" + shift + "', PROD.to_date('" + in_time
+ "', 'dd/mm/yyyy hh:mi:ss AM'), " + " PROD.to_date('"
+ out_time + "', 'dd/mm/yyyy hh:mi:ss AM'), '" + prod_n
+ "', '" + body_n + "', " + " '" + car_n + "', '"
+ fire_n + "', '" + user_cd + "', PROD.to_date('" + sysdt
+ "', 'dd/mm/yyyy hh:mi:ss AM'), " + " 'CGE', '" + tot_qty + "'"
+ " , '"+RSN_N_SQL+"', 'Y' ) " ;
//ORA
query_td_ora = "INSERT INTO PR_OPR_TD" + " ("
+ " COMP_N, YR_N, TRN_N, TRN_C, "
+ " TRN_D, TRNDTL_N, SRNO_C, QTY, SMPL_RMK, "
+ " OPR_N, OPRGRP_N, NXT_OPR, REJTYP_N, "
+ " REJ_QTY, STATUS, SRNO_N, DECK_NO, "
+ " CREATED_UID , CREATED_D , " +
" TRNTYP, PREV_OPR, GOOD_QTY, PREV_STATUS, "
+ " BODY_N, BARCODE_NO, PREVTRNDTL_N, PROD_N, " + "" +
" glzsrno_c, good_wt, exttrn_d," +
" glztrn_d, unit_wt, layer_n, zones_n, SOTTRN_D, ASSBLD_STS" +
" )" + " VALUES"
+ "(" + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
+ "', 'CGE" + TRN_C + "', " + " to_date('" + trn_d
+ "', 'dd/mm/yyyy'), '" + trndtl_n + "', '" + srno_c + "', '" + accept_qty
+ "', '" + barcode_no + "', " + " '" + OPR_N + "', '"
+ OPRGRP_N + "', '" + NXT_OPR + "', '" + REJTYP_N + "', "
+ " '" + rej_qty + "', '" + status + "', '" + srno_n
+ "', '" + deck_no + "', " + " '" + user_cd
+ "', to_date('"+ora_sysdt+"', 'dd/mm/yyyy HH24:MI:SS'), "
+ " 'CGE', '" + opr_n + "', '" + good_qty + "', '"
+ PREV_STATUS + "', " + " '" + body_n + "', '"
+ barcode_no + "', '" + TRNDTL_N + "'" + " , '"+prod_n+"', " +
" '" + glzsrno_c + "', '" + good_wt + "', to_date('" + exttrn_d + "' , 'dd/mm/yyyy'), " +
" to_date('" + glztrn_d + "' , 'dd/mm/yyyy'), '" + unit_wt + "', '" + layer_n + "', " +
" '" + zones_n + "', to_date('"+SOTTRN_D+"', 'dd/mm/yyyy'),'"+ASSBLD_STS+"' " +
")";
//SQL MD20230302
query_td_sql = "INSERT INTO PROD.PR_OPR_TD" + " ("
+ " COMP_N, YR_N, TRN_N, TRN_C, "
+ " TRN_D, TRNDTL_N, SRNO_C, QTY, SMPL_RMK, "
+ " OPR_N, OPRGRP_N, NXT_OPR, REJTYP_N, "
+ " REJ_QTY, STATUS, SRNO_N, DECK_NO, "
+ " CREATED_UID , CREATED_D , " +
" TRNTYP, PREV_OPR, GOOD_QTY, PREV_STATUS, "
+ " BODY_N, BARCODE_NO, PREVTRNDTL_N, PROD_N, " + "" +
" glzsrno_c, good_wt, exttrn_d," +
" glztrn_d, unit_wt, layer_n, zones_n, FLAG, SOTTRN_D, ASSBLD_STS" +
" )" + " VALUES"
+ "(" + " '" + comp_cd + "', '" + yr_n + "', '" + TRN_N
+ "', 'CGE" + TRN_C + "', " + " PROD.to_date('" + trn_d
+ "', 'dd/mm/yyyy'), '" + trndtl_n + "', '" + srno_c + "', '" + accept_qty
+ "', '" + barcode_no + "', " + " '" + OPR_N + "', '"
+ OPRGRP_N + "', '" + NXT_OPR + "', '" + REJTYP_N_SQL + "', "
+ " '" + rej_qty + "', '" + status + "', '" + srno_n
+ "', '" + deck_no + "', " + " '" + user_cd
+ "', PROD.to_date('" + sysdt + "', 'dd/mm/yyyy hh:mi:ss AM'), "
+ " 'CGE', '" + opr_n + "', '" + good_qty + "', '"
+ PREV_STATUS + "', " + " '" + body_n + "', '"
+ barcode_no + "', '" + TRNDTL_N + "'" + " , '"+prod_n+"', " +
" '" + glzsrno_c + "', '" + good_wt + "', PROD.to_date('" + exttrn_d + "' , 'dd/mm/yyyy'), " +
" PROD.to_date('" + glztrn_d + "' , 'dd/mm/yyyy'), '" + unit_wt + "', '" + layer_n + "', " +
" '" + zones_n + "', 'Y', PROD.to_date('"+SOTTRN_D+"', 'dd/mm/yyyy'), '"+ASSBLD_STS+"' "
+")";
//System.out.println(query_th_ora);
stmt_ora1.executeUpdate(query_th_ora);
//System.out.println(query_th_sql);
stmt3.executeUpdate(query_th_sql);
//System.out.println(query_td_ora);
stmt_ora1.executeUpdate(query_td_ora);
//System.out.println(query_td_sql);
stmt3.executeUpdate(query_td_sql);
}
catch (SQLException e)
{
dbcon.rollback();
dbcon_sql.rollback();
e.printStackTrace();
}
catch (Exception e)
{
dbcon.rollback();
dbcon_sql.rollback();
e.printStackTrace();
}
}
}
And some where in between Business Logic method I always got that exception.
And strange thing is that I always got this exception in my servlets only never in Databean.
I know the why this exception is raise but I don't understand the cause.
And second thing My tomcat service get also hang intermittently.
become completely unresponsive, need to restr in order to run the application again.
So request to all of u to please respond my query/que/issue and help me to get out of this mud bag. One thing to mention is my client machines runs on WiFi network, does it has any effect on the raising this issue?
Thanks a lot in advance

you can get this exception when try to use the PreparedStatemen while its close
This id detailed in this question

Related

sqlite JOIN query for multiple entries

My database has "diary" table and "food" table. In diary table I have following columns: date, food1,food2, ...food10. In food table I have: id, name, allergen1, allergen2, allergen3. So basically you log up to 10 foods a day to diary. Now I'm trying to build a query which would let me find last date that I've eaten food with allergen3. So that's the query I came up with, which you probably know is not working ;-)
Cursor findDateRes = db.rawQuery("SELECT date(" + DIARY_COL_DATE + ") FROM " + DIARY_TABLE
+ " INNER JOIN " + FOOD_TABLE + " food1 ON (food1." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F1 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food2 ON (food2." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F2 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food3 ON (food3." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F3 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food4 ON (food4." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F4 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food5 ON (food5." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F5 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food6 ON (food6." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F6 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food7 ON (food7." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F7 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food8 ON (food8." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F8 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food9 ON (food9." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F9 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food10 ON (food10." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F10 +
") WHERE 1 NOT IN (food1." + FOOD_COL_5 +
",food2." + FOOD_COL_5 +
",food3." + FOOD_COL_5 +
",food4." + FOOD_COL_5 +
",food5." + FOOD_COL_5 +
",food6." + FOOD_COL_5 +
",food7." + FOOD_COL_5 +
",food8." + FOOD_COL_5 +
",food9." + FOOD_COL_5 +
",food10." + FOOD_COL_5 +
") ORDER BY date(" + DIARY_COL_DATE + ") ASC LIMIT 1", null);
"FOOD_COL_1" stands for id and "FOOD_COL_5" stands fro allergen3.
Thank you for looking. Any input will be appreciated.

Imp code modification required

Following code needs modification can we use constants files if yes then how we can segregate string in separate files so that loop continues then performance is not impacted
for (int j = 0; j < subconfigListRLC.size(); j++) {
StringBuffer sqlQuery = new StringBuffer();
test = (SubConfigurationDetailsObject) subconfigListRLC.get(j);
if (test.getFlag().equalsIgnoreCase("T")) {
sqlQuery = sqlQuery.append("update SUB_CONFIG set TSTED = "
+ test.getSubConfigurationIndexNo() + " , Q_SUB_INDX = 0 "
+ "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
+ "AND MODEL_YEAR = '" + modelYear + "' "
+ "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
+ "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
+ "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
+ "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
+ "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
+ "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
} else if (test.getFlag().equalsIgnoreCase("U")) {
sqlQuery = sqlQuery.append("update SUB_CONFIG set Q_SUB_INDX = "
+ test.getSubConfigurationIndexNo() + " "
+ "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
+ "AND MODEL_YEAR = '" + modelYear + "' "
+ "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
+ "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
+ "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
+ "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
+ "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
+ "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
}
//System.out.println("Query----------->"+sqlQuery.toString());
processor.getUpdateAccessor().executeUpdateSql(sqlQuery.toString());
}
Use a Java PreparedStatement in order to build the query. You can then store the query string for the PreparedStatement in a properties file. Read the two possible query strings into variables before entering the loop (in fact you may want to build both PreparedStatements before entering the loop - depending on whether you always use them both). You can then call clearParamaters, then set your new parameters, execute, repeat.
As you asked for the exact details, something like this. Search for javadocs on PreparedStatement. Javadocs are always worth reading.
String sql = "update SUB_CONFIG set TSTED = ? , Q_SUB_INDX = ? " +
"where BASE_ENG_KEY = ? " +
"AND MODEL_YEAR = ? " +
"AND RLHP_LVW = ? " +
"AND LVW_TEST_WT_WO_CONT = ? " +
"AND INERTIA_WT_CLASS = ? " +
"AND TEST_GROUP_ID = ? " +
"AND ENGINE_CODE = ? " +
"AND AXLE_RATIO = ?");
PreparedStatement statement = connection.createPreparedStatement(sql);
for (SubConfigurationDetailsObject test: subconfigListRLC) {
if (test.getFlag().equalsIgnoreCase("T")) {
statement.setIntParam(1, test.getSubConfigurationIndexNo());
statement.setIntParam(2, 0);
} else if (test.getFlag().equalsIgnoreCase("U")) {
statement.setIntParam(1, 0);
statement.setIntParam(2, test.getSubConfigurationIndexNo());
} else {
continue;
}
statement.set...Param(3, ...);
...
statement.executeUpdate();
}
You may probably only need one PreparedStatement, and the result is indeed faster.
BTW. better use StringBuilder instead of StringBuffer.

SQLite select, join and where clause problems

Im trying to populate a listview with data from 3 tables using join, and one table to check wether a user is part of a group. But im having trouble getting all the groups for a given user. Heres the tables
Group table
_id | group_name | group_description | group_image
User table
_id | user_name | user_image
Group participants table (this table determines which users are part of which groups)
_id | user_id_foreign | group_id_foreign
Activityobject table
_id | target_id | user_id_foreign | target_type | time | type
Here is the sql statement im trying to get to work:
Cursor c = database
.rawQuery(
"SELECT groups.*, activityobject.time, activityobject.type, user.user_name, user.user_image "
+ "FROM activityobject "
+ "JOIN user "
+ "ON user._id = activityobject.user_id "
+ "JOIN groups"
+ "ON groups._id = activityobject.target_id "
+ "WHERE groups._id IN(SELECT group_id_foreign FROM group_participants WHERE user_id_foreign = ?)"
+ "AND activityobject.target_type = 0 "
+ "GROUP BY groups._id "
+ "ORDER BY activityobject._id",
new String[] { String.valueOf(userId) });
The result i get right now are all the groups that has an activityobject, but not the ones without.
I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell.
Thanks in advance!
EDIT:
An image of the tables im refering to
Heres my insert statements with data in the tables
//Activity object table
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (1, 2, 1, 1, 'Alex gruppe', 0, '07-05-13', 1)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (2, 2, 1, 2, 'Jeremys gruppe', 0, '08-06-13', 2)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (3, 2, 1, 2, 'Jeremys gruppe', 1, '09-07-13',3)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (4, 2, 1, 2, 'Jeremys gruppe', 0, '10-08-13', 4)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (5, 2, 1, 1, 'Alex gruppe', 0, '11-09-13', 5)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (6, 2, 1, 1, 'Alex gruppe', 0, '12-10-13', 6)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (7, 2, 1, 1, 'Alex gruppe', 1, '13-11-13', 7)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (8, 2, 1, 1, 'Alex gruppe', 0, '14-12-13', 8)");
// Group table
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (1,'alex gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (2,'jeremy gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (3,'ole gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (4,'egon gruppe', 'mega awesome gruppe', null);");
// User table
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (1, 'Alex', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (2, 'Peter', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (3, 'Jeremy', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (4, 'Søren', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (5, 'Ole', null, 0)");
// Group participants table
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(1, 1, 1)");
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(2, 1, 4)");
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(3, 2, 2)");
"I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell."
You have to use a LEFT JOIN then.
select g.*, a.time, a.type, u.user_name, u.user_image
from groups g
join group_participants gp on gp.group_id_foreign = g._id
left join activityobject a on g._id = a.target_id and a.target_type = 0
left join user u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;
If you just want to select the last activity object per group (if any), you can add that to the join condition, too:
select g.*, a.*
from groups g
join group_participants gp on gp.group_id_foreign = g._id
left join activityobject a
on g._id = a.target_id and
a.target_type = 0 and
a._id in (select max(_id) from activityobject group by target_id)
left join users u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;
Just keep in mind that you have to add all filtering criteria for activityobjects to the join condition of the left join, not to the where clause of your main query. Otherwise, you will surpress rows from the result where no matching activityobject can be found, instead of filling up the columns with nulls.

Cannot insert data into Database( Syntax error into INSERT INTO Statement)

private void addCustomerActionPerformed( ActionEvent e )
{
String Query;
Query = "INSERT INTO Membership VALUES"+ "(' " +memberID_a.getText() + " ' , ' " +memberName_a.getText() + " ' , ' " + icNo_a.getText() + " ', ' " + gender_a.getText() +" ' , '" + birthday_a.getText() +" ' , ' " + telephoneNo_a.getText() + " ',' " + mobileNo_a.getText() + " ' , ' " + email_a.getText() + " ' , ' " + address_a.getText() + " ' , " + postalCode_a.getText() + " ,' " + state_a.getText() + " ' , '" + country_a.getText() + " ' , " + memberPoint_a.getText() + ");";
try {
stmtInsert = conn.createStatement();
stmtInsert.executeUpdate(Query);
} catch(Exception ex){
JOptionPane.showMessageDialog(null,"ERROR"+ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
}
}
I am getting the error of
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
Anyone help me with this problem ? thanks
This will work
Query ="INSERT INTO Membership VALUES ( " +memberID_a.getText() + " , " +memberName_a.getText() + " , " + icNo_a.getText() + " , " + gender_a.getText() +" ," + birthday_a.getText() +"," + telephoneNo_a.getText() + " , " + mobileNo_a.getText() + " , " + email_a.getText() + " , " + address_a.getText() + " , " + postalCode_a.getText() + ", " + state_a.getText() + " ," + country_a.getText() + "," + memberPoint_a.getText() + ")";

java update query for paradox database

i'm trying to update paradox table from java but i'm getting an Exception
java.sql.SQLException: [Microsoft][ODBC Paradox Driver] Operation must use an updateable query.
java.sql.SQLException: [Microsoft][ODBC Paradox Driver] Operation must use an updateable query.
java.sql.SQLException: [Microsoft][ODBC Paradox Driver] Operation must use an updateable query.
I'm using this code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Connection paradoxCon = paradox.createConnection();
Results res = new Results();
res.getAll();
if (res.allRes.isEmpty()) {
JOptionPane.showMessageDialog(mainPanel, "There are no Finished or Postponed Games!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
int y = res.allRes.size();
for (int x = 0; x < y; x = x + 1) {
try {
if (res.getAll().get(x).Reversed == 0) {
if (res.getAll().get(x).Status.equals("Fin")) {
String sql = "UPDATE Kvote SET _45_d = '" + res.getAll().get(x).HThome + "', _45_g = '" + res.getAll().get(x).HTaway + "', _90_d= '" + res.getAll().get(x).FThome + "', _90_g = '" + res.getAll().get(x).FTaway + "', Ok='Y' WHERE Kolo = '" + res.getAll().get(x).tRound + "' AND Sifra='" + res.getAll().get(x).TID + "'";
PreparedStatement ps = paradoxCon.prepareStatement(sql);
ps.executeUpdate();
}
if (res.getAll().get(x).Status.equals("Post")) {
String sql = "UPDATE Kvote SET _45_d = '" + res.getAll().get(x).HThome + "', _45_g = '" + res.getAll().get(x).HTaway + "', _90_d= '" + res.getAll().get(x).FThome + "', _90_g = '" + res.getAll().get(x).FTaway + "', Ok='O' WHERE Kolo = '" + res.getAll().get(x).tRound + "' AND Sifra='" + res.getAll().get(x).TID + "'";
PreparedStatement ps = paradoxCon.prepareStatement(sql);
ps.executeUpdate();
}
}
if (res.getAll().get(x).Reversed == 1) {
if (res.getAll().get(x).Status.equals("Fin")) {
String sql = "UPDATE Kvote SET _45_d = '" + res.getAll().get(x).HTaway + "', _45_g = '" + res.getAll().get(x).HThome + "', _90_d= '" + res.getAll().get(x).FTaway + "', _90_g = '" + res.getAll().get(x).FThome + "', Ok='Y' WHERE Kolo = '" + res.getAll().get(x).tRound + "' AND Sifra='" + res.getAll().get(x).TID + "'";
PreparedStatement ps = paradoxCon.prepareStatement(sql);
ps.executeUpdate();
}
if (res.getAll().get(x).Status.equals("Post")) {
String sql = "UPDATE Kvote SET _45_d = '" + res.getAll().get(x).HTaway + "', _45_g = '" + res.getAll().get(x).HThome + "', _90_d= '" + res.getAll().get(x).FTaway + "', _90_g = '" + res.getAll().get(x).FThome + "', Ok='O' WHERE Kolo = '" + res.getAll().get(x).tRound + "' AND Sifra='" + res.getAll().get(x).TID + "'";
PreparedStatement ps = paradoxCon.prepareStatement(sql);
ps.executeUpdate();
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
res.getAll().clear();
} catch (ParseException ex) {
Logger.getLogger(AutoResultsImporterView.class.getName()).log(Level.SEVERE, null, ex);
}
}
I Solve the problem using INTERSOLV 3.11 32-BIT ParadoxFile (*.db) driver
I created an System DSN Data Source Using this driver and connect to this Data Source

Categories

Resources