# Java exception when inserting into postgres db

**URL:** <https://forum.inductiveautomation.com/t/java-exception-when-inserting-into-postgres-db/52707>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [November 4, 2021, 3:13am UTC](https://forum.inductiveautomation.com/t/java-exception-when-inserting-into-postgres-db/52707 "2021-11-04T03:13:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnd\_tk9](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rnd\_tk9](https://forum.inductiveautomation.com/u/rnd_tk9)\
**Post date:** [November 4, 2021, 3:13am UTC](https://forum.inductiveautomation.com/t/java-exception-when-inserting-into-postgres-db/52707/1 "2021-11-04T03:13:29Z")

</div>

When i execute the code below, I get the following error  
_B\_CONNECTION = ‘data\_collector\_db\_connection’  
level = ‘WARNING’  
message=‘MyModule:: my message’  
query = “INSERT into my\_schema.logs (timestamp, module, level, message) VALUES(now(),?,?,?)”  
results = system.db.runPrepUpdate(query,[module, level, message], DB\_CONNECTION)_

\<type ‘java.lang.Exception’\> : java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT into equipments.logs (timestamp, module, level, message) VALUES(now(),?,?,?), [MyModule, WARNING, MyModule:: my message], data\_collector\_db\_connection, , false, false)")

‘data\_collector\_db\_connection’ is the name of the database _connection_ (not db name) and its status is ‘Valid’. The table and column names are all correct.

Are there some other configuration i need to make in order to allow the script to write to the db? I can read/query the db fine, and the db userid has select/insert/update/delete privileges. I made sure the table and column names are all correct.

TIA

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [November 4, 2021, 3:21am UTC](https://forum.inductiveautomation.com/t/java-exception-when-inserting-into-postgres-db/52707/2 "2021-11-04T03:21:12Z")

</div>

> [@rnd\_tk9](#):
>
> query = “INSERT into my\_schema.logs (timestamp, module, level, message) VALUES(now(),?,?,?)”  
> results = system.db.runPrepUpdate(query,[module, level, message], DB\_CONNECTION)

Welcome!

Try:

```auto
query = “INSERT into my_schema.logs (timestamp, module, level, message) VALUES(?,?,?,?)”
results = system.db.runPrepUpdate(query,[now(),module, level, message], DB_CONNECTION)

```

Also your first line says "B\_CONNECTION" not "DB\_CONNECTION", assume this is copy and paste error but if not, fix that too.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 4, 2021, 10:49am UTC](https://forum.inductiveautomation.com/t/java-exception-when-inserting-into-postgres-db/52707/3 "2021-11-04T10:49:14Z")

</div>

@Matrix_Engineering is on the right path, but it still won’t work with now().  
EDIT: or rather, now() would have to be a defined function in the db in the first instance, or a defined function in the second instance.

EDIT2: I also noticed that MyModule is not defined in the script you posted. As a tip, when you look at the error messages, there should be a ‘Caused by’ line. That should give an extra hint as to what is going on.

```auto
DB_CONNECTION = 'data_collector_db_connection'
level = 'WARNING'
message='MyModule:: my message'
now = system.date.now()

query = "INSERT into my_schema.logs (timestamp, module, level, message) VALUES(?,?,?,?)"
results = system.db.runPrepUpdate(query,[now, MyModule, level, message], DB_CONNECTION)

```
