I have a JSONB column in my postgres table, I want to update the some fields in that column using JDBC.
Please help to for a JDBC query to update JSONB column.
I guess you would need to first connect by creating the object 'conn' in your code base with something of the like, more details on this step here.
// create a Statement from the connection
Statement statement = conn.createStatement();
and then embed the SQL query in the JDBC as usual in Java yes.
And write the appropiate query, this should work for example in order to leave null all entries with marketplace equal to US:
// update the data
statement.executeUpdate("UPDATE mytable SET App = jsonb_set(App, '{marketplace}', '""') where data ->>'marketplace' = 'US' ");
I have to execute multiple insert queries using JDBC for which I am trying to execute batch statement. Everything works fine in my code but when i try to see values in the table, the table is empty.
Here is the code :
SessionImpl sessionImpl = (SessionImpl) getSessionFactory().openSession();
Connection conn = (Connection) sessionImpl.connection();
Statement statement = (Statement) conn.createStatement();
for (String query : queries) {
statement.addBatch(query);
}
statement.executeBatch();
statement.close();
conn.close();
And the
List<String> queries
contains insert queries like:
insert into demo values (null,'Sharmzad','10006','http://demo.com','3 Results','some values','$44.00','10006P2','No Ratings','No Reviews','Egypt','Duration: 8 hours','tour','Day Cruises');
And the table structure is like:
create table demo ( ID INTEGER PRIMARY KEY AUTO_INCREMENT,supplierName varchar(200),supplierId varchar(200),supplierUrl varchar(200),totalActivities varchar(200),activityName varchar(200),activityPrice varchar(200),tourCode varchar(200),starRating varchar(200),totalReviews varchar(200),geography varchar(200),duration varchar(200),category varchar(200),subCategory varchar(200));
No exception is thrown anywhere but no value is inserted. Can someone explain?
Most JDBC drivers use autocommit, but some of them do not. If you don't know, you should use either .setAutoCommit(true) before the transaction or .commit() after it..
Could be a transaction issue. Perhaps you're not committing your transaction? If so, then it is normal not to see anything in the database.
You can check if this is the case by running a client in READ_UNCOMMITTED transaction mode, right after .executeBatch(); (but before close()) and see if there are any rows.
You don't should assign a value to ID add supply all the others columns name
insert into demo
(
supplierName
,supplierId
,supplierUrl
,totalActivities
,activityName
,activityPrice
,tourCode
,starRating
,totalReviews
,geography
,duration
,category
,subCategory
)
values (
'Sharmzad'
,'10006'
,'http://demo.com'
,'3 Results'
,'some values'
,'$44.00'
,'10006P2'
,'No Ratings'
,'No Reviews'
,'Egypt'
,'Duration: 8 hours
','tour'
,'Day Cruises'
);
and add commit to your code
I need a java application that can export some data from a Oracle database and write it to a Excel file everyday. I am really new to JAVA so I am making this app step by step.
First to all I'm going to show the database schema (simplified version):
GLOBAL (allocated in bar.domain.es)
-DATABASE1:
TABLE A
TABLE B
TABLE C
-DATABASE2:
TABLE 1
TABLE 2
One part of my code is:
//Loading the driver
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver Loaded");
//Connecting to Oracle Database
java.sql.Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
System.out.println("Connection Success");
//Creating statement
Statement stat = con.createStatement();
//Creating the query string
String query ="SELECT count(*) FROM TABLE2 WHERE DATE=150603 AND ID=238";
// Creating the statement to execute the Query
ResultSet rs = stat.executeQuery(query);
where DBURL is: "jdbc:oracle:thin:#bar.domain.es:1521:XE"
With this code I get the message Connection Success so my app is connected to the database schema. However, in this schema there are several databases with several tables on each so my problem comes when I try to launch the query. The program doesn't find TABLE2 which is a table of the DATABASE2. I think that I should specify in someway that I want to search this TABLE2 in DATABASE2 but I don't know how.
You can specify in the query what database the table is in
String query ="SELECT count(*) FROM DATABASE2.TABLE2 WHERE DATE=150603 AND ID=238";
CREATE USER Person identified by 2012;GRANT ALL PRIVILEGES TO Person;
These statements are successfully executed by Oracle 11g (GUI). But, when I copy and paste the above statement exactly and try to execute it by using executeUpdate(String sql), I get the exception below. Why?
java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
You should not give a two different SQL statements as one. There is no way that you can JDBC driver will execute two statements passed as one string.
try to execute them as
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE USER Person identified by 2012");
stmt.executeUpdate("GRANT ALL PRIVILEGES TO Person;");
That should do. Cheers.
Dependend on your database jdbc driver, the driver will not support executing two statements in one "executeUpdate". You have to do something like:
Statement stmt = conn.createStatement();
for(String statement : statements) {
stmt.addBatch(statement);
}
stmt.executeBatch();
I am trying to retrieve data from MySQL database using eclipse.I am using the same code of JDBC as for a java application...but it does work.
You can do as follows:
Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:mysql://localhost/database_name", username,password);
// Statements allow to issue SQL queries to the database ; that's why we need to create one.
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement.executeQuery("select * from TableName;");
while (resultSet.next()) { //retrieve data
String data = resultSet.getString("column_name");
...
}
Please make sure that your database connection is created successfully then apply queries to retrieve data from database. If your connection is not created then you need to check you driver setting or mysql password setting.
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/database_name",username,password");
Connection class having method like 'createstatement()', with help of this we can query from database.
Statement statement = con.createstatement();
resultSet=statement.executeQuery("select * from tablename");
And finally retrive the data from the 'resultSet' object.