I have a query which batch of sql commands in one string,
String SQLQuery =
"CREATE VOLATILE TABLE v1;
CREATE VOLATILE TABLE v2;
INSERT INTO v1;
//do stuff
INSERT INTO v2;
//do stuff
UPDATE xx;
//do stuff
DROP TABLE v1;
DROP TABLE v2;"
I tried to execute them with executeUpdate(), executeBatch() both of them fails with java jdbc client, which works in Teradata client.
Error mesg:
[TeraJDBC 15.10.00.35] [Error 3576] [SQLState 25000] Data definition not valid unless solitary.
Java code:
PreparedStatement stmt10 = null;
try {
stmt10 = conn.prepareStatement(SQLQuery);
//stmt10.executeUpdate();
stmt10.executeBatch();
//conn.commit();
} catch (Exception ex) {
throw new Exception(ex.getMessage());
} finally {
if (stmt10 != null) {
stmt10.close();
}
}
how can i do this?
In teradata you cannot execute DDL statements together. It's not a problem with the JDBC or the java code - just the way you want to use teradata. That is what that error is basically saying:
http://teradataerror.com/3576-Data-definition-not-valid-unless-solitary.html
So you need to send the separate the DDL statements in separate transactions with a separate requests. If it was multiple updates and selects it would work just fine but create table cannot be executed like that.
I believe it is due to the fact that any DDL request MUST be commited before actually happening. And if you use it like that it is not. I think it is actually OK to have DDL in a multiple commands query if it is only one and it is the last statement .
Related
I need to create a web application that allows users to create a database using ER diagrams and then perform basic CRUD operations on it. I have already done most of the job, how it's work:
As you can see after create tables i have ready to use SQL code (right bottom corner).
And now i need to execute this code from java.
I am using Java (1.8), Oracle(11gR2), Hibernate (5.4.9.Final)
Here is my code which is used to execute native queries (i am loading sql from file):
Session session = hibernateFactory.openSession();
String query = null;
try {
query = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("test.sql").toURI())));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
String finalQuery = query;
session.doWork(connection -> connection.prepareStatement(finalQuery).execute());
And here is the question:
Is this possible to execute this whole code at once?
I can execute this:
but when i will put another table, then i will got this error:
Its seems like hibernate don't like character: ";"
Is it possible to fix this and force this prepareStatement to execute whole code sql? Or i will need to do it in parts?
I'm pretty new to Hive and HDFS, but I have managed to make a functioning HiveClient in java, that successfully connects and performs queries on my HDFS server.That is, all queries except select statements.
My code looks like this:
Statement stmt;
ResultSet res;
try {
stmt = con.createStatement();
res = stmt.executeQuery("select * from my_table");
while (res.next()) {
System.out.println(res.getString(1));
}
res.close();
stmt.close();
con.close();
} catch (SQLExceptionex) {
ex.printStackTrace();
}
When I run it, the error is this:
java.sql.SQLException: Query returned non-zero code: 9, cause: FAILED: Execution Error, return code -101 from shark.execution.SparkTask
at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:194)
at se.HiveClient.doQuery(HiveClient.java:56)
at se.HiveClient.main(HiveClient.java:82)
but if I instead do a create table or show tables, it runs perfectly. Could there be a case of missing configuration or privileges? Or something else entirely?
Any ideas as to where I may have done wrong or missed something is most appreciated.
It should be a permissions issue.
Creation of tables or showing tables require hive to get metadata from its database.
Actual selection of data requires to read a file. Check what are the permissions given to the file.
Hive would probably be querying as the hive user and hence either should be a owner of the file or should be in the right group.
This seems to be a similar issue: http://forums.pentaho.com/archive/index.php/t-89586.html
I am working with AWS RDS (specifically mysql) and I am using SQL Workbench/J as a GUI tool. My server side code written in Java and here is my code:
Insert code:
try {
Statement myStatement = insertConnectionObject.createStatement();
myStatement.executeUpdate("INSERT INTO friends VALUES('buddy', '15', '123');");
myStatement.close();
} catch(Exception ex) {
// code for handling exceptions
} finally {
myStatement.close();
insertConnectionObject.close();
}
After that, I call the select code from the same table:
try {
Statement myStatement = selectConnectionObject.createStatement();
ResultSet returnedFriends = myStatement.executeQuery("SELECT * FROM friends;");
//unfortunately, the returnedFriends will not return the new inserted value 'buddy'
} catch(Exception ex) {
// code for handling exceptions
} finally {
myStatement.close();
insertConnectionObject.
unfortunately, the returnedFriends will not return the new inserted value 'buddy'.
If I will click the 'commit any pending database changes' button in the SQL Workbench/J GUI tool, and then run the select statement, the new value 'buddy' will return.
What have I tried until now?
Use the same connection object for both insert and select.
Open and close the connection after the insert command, and after every select command.
disable the auto commit and try to commit manually.
Inserting via code, and then selecting directly from the DB.
Have you tried setAutoCommit(true) on the connection, just in case it isn't?
Also, if your select is just to get a new key don't forget you can call myStatement.getGeneratedKeys() in with the update.
You should use executeQuery() to select . executeUpdate() returns nothing but int. It should give a compile time error, are you sure that the code is compiling rather than running last working version?
executeUpdate(String sql) Executes the given SQL statement, which may
be an INSERT, UPDATE, or DELETE statement or an SQL statement that
returns nothing, such as an SQL DDL statement.
So change your select code as below:
ResultSet returnedFriends = myStatement.executeQuery("SELECT * FROM friends;");
My problem was as simple and annoying as can be - apparently, I had to close the Workbench GUI when working from the code, which is kind of wired and requires probably deeper investigation from the Workbench / AWS teams.
Anyways, after closing this interface, everything just worked.
Thanks for the help!
I am trying to create events using Java code with hibernate. I verified my syntax for CREATE EVENT in MySQL Workbench, and I verified my Java code with a simple UPDATE query. But when I am trying to use both it just doesn't work. I am not getting any error message, just that 0 rows were affected. My code is as follows:
String sql = "CREATE EVENT IF NOT EXISTS test_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 20 SECOND ON COMPLETION PRESERVE ENABLE DO UPDATE my_table SET last_error_message='my test' WHERE ID=17;";
session.beginTransaction();
SQLQuery query = session.createSQLQuery(sql);
int result = query.executeUpdate();
session.getTransaction().commit();
....
session.close();
thanks a lot
How do you know if any new events were created? You can try
show events from <SCHEME_NAME>;
This will show all the events that are registered to the given schema.try printing the session\statement warning stack...
Get jdbc connection from your session: How to get jdbc connection from hibernate session?
Use JDBC to execute DDL
...
Statement stmt = null;
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
// ...
} finally {
// close stmt
}
...
First, you need to make sure your database is prepared to execute an event. for that, you need to run the following command.
SHOW PROCESSLIST;
MySQL uses a special thread called event schedule thread to execute all scheduled events.
if you see your process list like the above picture. you need to run the below command to enable MySQL event execution.
SET GLOBAL event_scheduler = ON;
Then when you execute the "SHOW PROCESSLIST;" command you will see the below list which shows the specific thread for event execution in MySQL.
Now you can create your event using any MySQL client interface.
I have a MSSQL Database, and I have a stored procedure for any possible query, most of them just return a row of data with 3 columns or just execute an INSERT
How in java to connect to the DB and execute a stored procedure, and retrieve some data ?
A connection pool like DBCP makes a big difference. The connection time can be save this way.
Prepared statements can help the database to skip query parsing. The parsed statements will be cached.
Batch updates help when you're executing a statement repeatedly.
Setting the right fetch size is another optimization for queries.
Use the MSSQL JDBC Driver to create a connection to the database
In jdbc, you need to create a CallableStatement to execute the procedure. It's like this:
.
CallableStatement callable = null;
try {
String sqlCommand = "{call yourProcNameHere (?, ? /* ... */)}";
callable = conn.prepareCall(sqlCommand);
// ...
}
catch (SQLException e) {
// ...
}
finally {
/ ...
}
By reading and working through a JDBC Tutorial.