i need to create a standalone database app using java and HSQL, the constructor of the app creates the database and 2 tables.I wrote the following:
Connection connection =DriverManager.getConnection(
jdbc:hsqldb:file:D:\\prod \\prod,"SA","");
Statement statement1=connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statement1.executeQuery(query1);
statement1.close();
Statement statement2=connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
statement2.executeQuery(query2);
statement2.close();
connection.close();
aftre running the application, only the first statement has been executed and one table has been created without any SQL exceptions. any help.............best ergards
You shouldn't use executeQuery(text) for CREATE TABLE and other data definition statements.
Use executeUpdate(text) instead.
Related
I'm writing a simple database migration code in clojure using the new seancorfield/next.jdbc library.
How do I execute several SQL statements at once? The usecase is that I have a SQL file containing query code for migrating from one version to the next. next.jdbc/execute! only executes one statement as designed.
Whether you can execute multiple statements in a single JDBC operation is database-dependent. Some databases allow multiple statements in a single operation, separated by a semicolon. If the JDBC driver supports it, next.jdbc will also support it.
If your JDBC driver does not support it, you'll need to make multiple execute! calls. Some databases allow you to wrap multiple DDL operations in a transaction (some of them ignore the transaction and commit each DDL operation separately anyway), some databases explicitly disallow a transaction around DDL operations.
I need to insert data to 3 tables. I'm trying to use a SP. I know I could use Triggers instead but, for integrity reasons, I can't.
When I execute the INSERT statement in an independient Script, I works fine.
The problem is I'm trying to do the insert statement in the SP but it throws me
[SQL7008] error.
I don't know why it throws me this error only in the SP and not (using the same insert statement) in an independient script.
It's good to provide the DB2 version.
If it's DB2 for IBM i (aka iSeries, OS/400), then:
Received an SQL7008 error when attempting to INSERT data into an AS/400 table. "REXX variable '' contains inconsistent data".
As part of a refactor, I am trying to change database calls to use Spring 4.1.0.RELEASE in order to handle connections and exceptions and allow result sets to be passed between functions and classes.
I've gotten my MS SQL Server stored procedure calls working fine, but when I tried to execute an Oracle stored procedure, I got the following error message:
2014-11-13 15:39:35,836 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /EmailServiceLayer/EmailServletClient/springtest/123:
org.jboss.resteasy.spi.UnhandledException: org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback;
bad SQL grammar [{call SPRING_JDBC_TEMPLATE_TEST()}]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'SPRING_JDBC_TEMPLATE_TEST' must be declared
I simplified the problem by writing a couple very simple stored procedures that write to a test table: one that takes a parameter and writes and one that takes no parameters and just writes a hardcoded value. These procedures are in the INV schema, which is the same user that my datasource is configured to use.
Eventually I tried running it on my own personal schema, using a datasource configured to use my personal credentials, and it succeeded. I had an admin grant execute all on the procedures in INV, but still no luck. I've confirmed that I can successfully execute simple inline inserts on the INV schema using Spring JdbcTemplate.execute().
My first attempts were using the JdbcTemplate with a CallableStatementCreator of my own definition. I then tried using SimpleJdbcCall, which is what I found out worked on my personal schema. Both ways give the same error message on the INV schema. Here is the code for my latest attempt:
SimpleJdbcCall caller = new SimpleJdbcCall(alex3InvTemplate).withProcedureName("spring_jdbc_template_test");
MapSqlParameterSource paramMap = new MapSqlParameterSource();
paramMap.addValue("p_testval", testval);
Map<String, Object> result = caller.execute(paramMap);
and my test proc:
create or replace
procedure spring_jdbc_template_test
(
p_testval IN number
)
as
begin
insert into jdbc_template_test_table values(p_testval);
commit;
end;
My application is running on a Wildfly 8.0.0.Final server. The datasource configurations for the 2 schemas are exactly the same except for the credentials used to log in. I am able to execute the procedures in INV using the same datasource with a basic JDBC CallableStatement and I've confirmed that I can run them in SQL Developer.
Thanks in advance for any help.
Turns out I made a silly mistake (typo) and was still using an old datasource and thus a different user than INV. Thanks to Dmitry for making me realize this. Still not sure why I'm unable to execute the procedure from a different user using the Spring framework when I was able to do so using a basic JDBC CallableStatement. It works with the INV datasource though and that's a satisfactory solution for me
I am new to Pentaho data integration tool.I am trying to move data from a source table into target table ... both is SQL Server. The tables are identical and has an identity column.
Tried many options but ... it gives an error every time saying "Indentity insert is set to OFF"
Tried introducing a hop inbetween to execute a SQL statement to "SET identity_insert tblname ON" .. still dint work.
Any suggestions would be highly appreciated.
Thanks.
Putting that in a hop certainly wont work, because PDI/kettle uses a connection(s) per step. You need to put that setting in the advanced options of the database connection and then you should be ok - it will then be used for all instances of that database connection.
Also make sure you "share" your database connections, otherwise if you create them from hand in every transformation you'll need to apply that setting to every single database connection in each transformation. ( Unless you're using a database or EE repository in which case the connections are centralised so you're ok )
One other thing you can try is to remove the identity columns from the select you are using to pass from the source to the destination.
This way, you will make sure that SQL will create a new identity for each one of the rows intead of trying to insert them,
You should add a command after db connection established.
does statement object contain the session id the database returns for the current session? What does a resultset
contain?
To the best of my knowledge, no, Statements do not have session IDs. It seems like the Java API specifications for the Statement class backs that up. Basically, Statements are used to execute SQL statements by specifying a SQL query through the execute method.
A ResultSet is used to retrieve results which are returned by executing a query via a Statement or PreparedStatement.
The JDBC(TM) Database Access trail of The Java Tutorials contains some information on these topics. The following sections may be of interest:
Lesson: JDBC Basics
Updating Tables
Retrieving Values from Result Sets