I have created 'smphas' table in my database. To retrieve the data, I write this code:
function goBack() {
window.location.replace("dashboardproject.jsp");
}
function getQuarter(phase){
if(phase==""){
alert("PLEASE CHOOSE PHASE");
} else {
document.getElementById("phase").value = phase;
constructionprogress.submit();
}
}
function selected(){
$('#phaseCons').val('<%=phase%>');
}
Mysql command is:
public List<String[]> constPeriodPhase (String projCde,String co_no){
System.out.println("/*** constPeriodPhase ***/");
String sql = "SELECT distinct smh_phase_num, smh_phase_nme FROM c"+co_no+".smphas WHERE smh_proj_cde = '"+projCde+"'";
System.out.println("Execute = "+sql);
return super.execListStrArr(sql,false);
}
However, the error is:
/* constPeriodPhase */
Execute = SELECT distinct smh_phase_num, smh_phase_nme FROM cnull.smphas WHERE smh_proj_cde = 'null'
nullnulle-Solicitor[cnull.smphas]
2020-03-12 09:53:58.165 SGT EXECUTE: SELECT distinct smh_phase_num, smh_phase_nme FROM cnull.smphas WHERE smh_proj_cde = 'null'
java.sql.SQLSyntaxErrorException: Unknown database 'cnull'
How can i retrieve the database from mysql?
Unknown database 'cnull'
with a query:
...FROM c"+co_no+".smp
mean co_no is null when the function was called.
Related
I have a SELECT statement like SELECT * FROM table where id=? and key IN (?,?..?).
For INSERT,UPDATE we have this batchWithParams, how can I do this for SELECT. I am using JDBC driver for MySQL database. The array list is like so,
BATCH [[100,4,11], [150,4,12]]
Why not just convert the ArrayList into String? Like this:
suspend fun getUsersByEmail(emails: Iterable<String>): List<User> {
return mysqlPool.getConnectionAwait().use { conn ->
conn.query(
"""
SELECT * FROM users
WHERE email IN (${emails.joinToString(separator = ", ") { "'$it'" }})
"""
).executeAwait().map { row ->
User(
row.getLong("id"),
row.getString("email")
)
}
}
}
I am insert data in db is good. The problem is after inserted data i am trying to get last inserted data primary key column. But i can't how to solve this problem.
note : i am using jpql , Thanks
public void insertData(String hcpHceId) {
EntityManager em = getEntityManager();
em.getTransaction().begin();
// Query query = em.createNativeQuery("INSERT INTO mdm_id (hcp_hce_id) VALUES(:id)" );
Query query = em.createNativeQuery("INSERT INTO mdm_id (hcp_hce_id) VALUES(?)");
query.setParameter(1, hcpHceId);
query.executeUpdate(); // Successfully inserted data
List list = query.setMaxResults(1).getResultList(); // Error line
em.getTransaction().commit();
} catch (Exception error) {
error.printStackTrace();
logger.error(error.getMessage());
} finally {
em.close();;
}
}
You are trying to retrieve the list from the same execute query. Query is an immutable object. You must create a new query with the proper select statement, so you will be able to retrieve data from database.
After the executeUpdate, create the new query using, v.g. (as you did not show your entity graph, using native sql):
Query retQuery = em.createNativeQuery("SELECT hcp_hce_id FROM mdm_id LIMIT 1");
String id = (String) retQuery.getSingleResult();
assert(id == hcpHceId);
The method to execute a prossidure in the bank was created, but the same is giving this error:
Caused by: java.lang.IllegalArgumentException: Unknown parameter registration name [movimentoId]
public static void validaPedidoCarga(EntityManager em, String movimentoId){
StringBuilder sql = new StringBuilder()
sql.append('BEGIN ')
sql.append('ADM_SGV.SGV_SFA_PK005.VALIDA_PEDIDO_CARGA(:movimentoId); ')
sql.append('END;')
Query query = em.createNativeQuery(sql.toString())
query.setParameter("movimentoId", movimentoId)
try{
query.()
em.close()
}catch(NoResultException ex){
ex.printStackTrace()
}
}
If a procedure is already created into DB then just call that procedure passing the correct argument like,
Query query = sf.getCurrentSession()
.createSQLQuery(
"CALL Procedure_Name(:movimentoId)")
.addEntity(ClassName.class)
.setParameter("movimentoId", "ur_Id");
List result = query.list();
To my Oracle Database I have registered a DatabaseChangeNotification with a SQL-Query. I like to get any changes on the defined TABLE.
DatabaseChangeRegistration changeRegistration;
Properties properties = new Properties();
properties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
properties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true")
changeRegistration = connection.registerDatabaseChangeNotification(properties);
DCNListener dcnListener = new DCNListener(this);
changeRegistration.addListener(this);
Statement statement = connection.createStatement();
((OracleStatement) statement).setDatabaseChangeRegistration(changeRegistration);
String sql = "SELECT * from TABLE";
statement.executeQuery(sql);
In my Listener I receive the DatabaseChangeEvents
public void onDatabaseChangeNotification(DatabaseChangeEvent databaseChangeEvent) {
TableChangeDescription[] tableChangeDescription = databaseChangeEvent.getTableChangeDescription();
QueryChangeDescription[] queryChangeDescription = databaseChangeEvent.getQueryChangeDescription();
for (QueryChangeDescription qcd: queryChangeDescription) {
String result = qcd.toString();
System.out.println(qcd);
}
}
tabelChangeDescription is null
My result is:
query ID=201, query change event type=QUERYCHANGE
Table Change Description (length=1): operation=[UPDATE], tableName=USER.TABLE, objectNumber=67385
Row Change Description (length=1):
ROW: operation=UPDATE, ROWID=AAAQc5AAHAAAAG/AAB
Is there any nice way to get the ROWID from the Changes row else than String-Parsing? I can't find any getRowId-Method on the QueryChangeDescription.
I found the was to get the RowId. From the queryChangeDescription you can get the TabeleChangeDesciptions which has nothing in Common with the TableChangeDecription from the event. If there are changes on more than one Table, these tables where listed in the Array.
Because I am registered to only one Table I don't have to iterate over the list.
After habing the the TableChangeDescriptionyou can get the RowChangeDescription for each changed row. From this you can get the RowId.
for (QueryChangeDescription queryChangeDescription : databaseChangeEvent.getQueryChangeDescription()) {
RowChangeDescription[] rowChangeDescriptions = queryChangeDescription.getTableChangeDescription()[0].getRowChangeDescription();
for (RowChangeDescription rowChangeDescription : rowChangeDescriptions) {
handleEvent(rowChangeDescription.getRowid());
}
}
I'm trying to fetch the last entity that was inserted into the database, which I thought would be a very simple thing to do, but every query i try results in some sort of exception to get thrown
The code im using is:
#Override
public DataStoreMark getLastMark() {
String selectQuery = "from Mark";
Query query = em.createNativeQuery(selectQuery, DataStoreMark.class);
try {
return (DataStoreMark) query.getSingleResult();
} catch (NoResultException e) {
log.error("Couldn't find any Marks in the DataStore.");
}
return null;
}
This code however throws a PesistenceException:
org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from milestone' at line 1
And there is definitely a record in the database.
Any ideas?
You can use either of createQuery() and createSqlQuery() if it does not work:
1.
String selectQuery = "from Mark";
Query query = em.createQuery(selectQuery);
return (DataStoreMark) query.list().get(0);
2.
String selectQuery = "select * from Mark";
SQLQuery query = (SQLQuery) em.createSQLQuery(selectQuery);
query.addEntity(DataStoreMark.class);
return query.list();
I think hibernate does not tell about last entity added to the database. Alternatively you can write the query specific to your db and run it using createSqlQuery() as shown above.