Exception caused while querying Hive Derby database via JDBC - jdbc

I am a new user to Hive and have just started using it for a project. I am having problem connecting to the database using java. It is explained as follows:
I ran Hive database on my machine that created a derby metastore_db using the Embedded Derby Driver. Now, I wish to query this metastore_db via a separate Java program for issuing queries.
I followed multiple tricks that I could find online but still am not able to get that working.
Snapshot of my Java file:
String url = "jdbc:derby:metastore_db;create=true";
String username = "APP";
String password = "mine";
String drivername = "org.apache.derby.jdbc.EmbeddedDriver";
Connection con = DriverManager.getConnection(url,username, password);
Statement stmt = con.createStatement();
res = stmt.executeQuery("describe " + tableName);
Running the java file:
I have placed it in the same folder as the metastore_db and then running it using the following command:
java -cp "derby-10.4.2.jar:." ConnectTesting
I get the following syntax exception:
java.sql.SQLException: Syntax error: Encountered "describe" at line 1, column 1.
Some of the things I have already tried:
url = "jdbc:derby:metastore_db;create=false";
url = "jdbc:derby://localhost:10000/metastore_db";
url = "jdbc:derby://localhost:1527/metastore_dbDB"; // By default port at which hive listens
The above two give error as:
No suitable driver found for jdbc:derby://localhost:10000/metastore_db
url = "jdbc:derby:<full_path_to_db>/metastore_db";
If I have my HIVE server already running, and then from another terminal I execute the java file, it says:
"Another instance of Derby may have already booted the database "
Can anyone kindly point out as to what am I doing wrong and how can I connect to the already existing database to issue queries.

That's a lot of different questions. Which one do you really need answered?
"describe" is a command implemented by the "ij" tool; it is not a SQL statement. So to run "describe", you have to run it from "ij" (or by using the RunScript utility to run "ij" within your application; you cannot run it directly from Statement.executeQuery().
In order to use the client-server driver, with the hostname:port syntax, you have to (a) use the derbyclient.jar rather than the derby.jar in your classpath, and (b) have the Network Server running.
And, you cannot have the same Derby (embedded) database open in multiple applications simultaneously, so while your application is running, you cannot run a second application against the same (embedded) database. If you need this functionality, you need to switch all your applications to using the client driver, and run the Network Server to provide multi-application access to your database.
Have you run the Derby tutorial yet? It covers most of these topics in more detail: http://db.apache.org/derby/docs/10.10/getstart/

Related

db2 load command fails using JDBC

I'm using JDBC to transfer data from a delimited file to a db2 database table. Initially, I encountered SQLCODE=-104, SQLSTATE=42601, so on further debugging I found this which referred me to call stored procedure SYSPROC.ADMIN_CMD.
I modified the call and tried running the procedure version, but I'm still getting the same error:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=CLIENT;LOAD;FROM, DRIVER=4.26.14
Error |
at com.ibm.db2.jcc.am.b7.a(b7.java:810)
Error |
at com.ibm.db2.jcc.am.b7.a(b7.java:66)
I'm not sure what exactly am doing wrong.
Code:
CALL SYSPROC.ADMIN_CMD('LOAD CLIENT FROM "<PATH_TO_FILE>" OF DEL MODIFIED BY COLDEL0X09 INSERT INTO <SCHEMA_NAME>.<TABLE_NAME> NONRECOVERABLE')
I ran the LOAD command on the db2 command prompt and it ran without any issues.
Db2 version: 11.5
The load client command is intended for use at a client workstation, not at a Db2-server, so sysproc.admin_cmd will reject the client keyword.
The stored procedure executes at the Db2-server , it does not have access to files that are stored at the client workstation.
Therefore any file you mention in parameters to sysproc.admin_cmd stored procedure must be relative to the Db2-server file-system and must be accessible (readable) to the Db2-instance owner account.
If your data-file is already located on the Db2-server, just reference its fully qualified filename and run the sysproc.admin_cmd procedure with the load command. Carefully check the documentation for the load command to understand all of the implications of using LOAD, especially if the target Db2-server is highly-available. This is an administration matter.
If your data-file is not already located at a Db2-server, then first copy the file to the Db2-server and retry with load (or slower import).
You can also run the import command via sysproc.admin_cmd when the data file is accessible to the Db2-instance owner on the Db2-server than runs that stored-procedure.
If your Db2-version is recent you can also consider the ingest command, refer to the documentation for details.
If your data file is located on a client workstation, and you are unable or unwilling to transfer it to a Db2-server, then you can use your local workstation Db2-client (if you installed a suitable Db2-client that includes the db2 CLP) to run a script/batch-file to perform the relevant commands. You cannot use jdbc for that specific purpose, although you can exec/shell out from java to run the required commands in one script (db2 connect to ... user ...using .., db2 load client ... , or db2 ingest ... or db2 import ...
If your target Db2-server is already at version 11.5 or higher then it should support insert from external table, and remote external table, and since INSERT is plain SQL then you can do that via jdbc.
Apart from the above, most DBAs would arrange for direct Db2-server to Db2-server transfers if both are Db2-LUW and have IP-connectivity and if security rules permit, this avoids the slow and insecure business of taking the data outside of Db2. That is not a programming matter, more an administrative matter.

Netbeans derby embedded error

I have created a GUI alp with a database. I have a problem with the Embedded connection.
I made this Embedded connection in Services tab > drivers > Java DB (Embedded) and connect using.
I enter the data as follows:
;create=true is what I write because it generally will not create a database folder in the app folder. I create a table and I put that URL in jer the code con = DriverManager.getConnection
When I start the app and when I enter the data in the respective fields that should fill in this database, the following error appears:
java.-sql.SQLSyntaxErrorException: Table/View 'nameTable' does not exists!
I first added derby.jar driver to the library and then I added Java DB driver which comes with Netbeans, but the error remains. The same error appears no matter what I do.
Here a code:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try { Connection con;
Connection db = null;
con = DriverManager.getConnection("jdbc:derby:testBase;create=true ", "app", "admin77");
Statement stmt = con.createStatement();
String Query ="INSERT INTO LINGU (NAME , CONCTRACTNO , EMAIL , PHONE , VIBER ) VALUES ('"+fNameLname.getText()+"' , '"+contTxt.getText()+"' , '")
stmt.execute(Query);
JOptionPane.showMessageDialog(null,"You have successfully added this vendor to the list of Lingu vendors.");
fNameLname.setText(null);
contTxt.setText(null);
emailTxT.setText(null);
phoneTxT.setText(null);
viberBox.setSelectedIndex(0);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
}
Table:
With a JDBC Connection URL like jdbc:derby:testBase;create=true, you are telling Derby to access a database named testBase, which is to be found in a folder named testBase relative to whatever is the current working directory when you run your program.
And you are also telling Derby that, if there is no database in a folder named testBase relative to whatever is the current working directory when you run your program, Derby should go ahead and create a new fresh empty database in that location.
This approach makes it easy to confuse yourself, because if you run the program once in one directory, and create some tables and load some data, and then run the program again in a different directory, it will seem like all your tables have vanished and all your data is gone.
(It isn't, it's just that you've now created two different databases, and you're confused about which database you're using with which run of your program.)
Note that, from the point of view of Derby, the NetBeans IDE is just another program that is using Derby, and the NetBeans IDE, itself, has its own "current working directory", and it is almost certainly not the same as the current working directory that you use when you run your program by hand.
Derby's ability to use relative filesystem paths for database locations, and its ability to quietly manufacture a new empty database on demand (create=true) are very nice features, but they can also be very confusing when you are just starting to learn Derby.
A fairly simple alternative is to avoid using relative database path names in your JDBC Connection URL, and instead always use an absolute database pathname, like:
jdbc:derby:/path/to/my/db/folder/testBase
Then you will find that you are always using the same database, and your data will be there the next time you go open that database.

How to launch H2 in server mode?

I'm trying to develop some app on H2 database, but the problem is I can't run JDBC queries and using web-interface of the database at the same time.
The exception log looks as follows:
org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-169]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
At H2 launch I've chosen Server mode but this doesn't help. The image is given below
http://gyazo.com/1c59fe77bf29e2061da1a0894e18a471
I found it, it takes 2 steps to complete it:
add String ";AUTO_SERVER=TRUE" to both jdbc String in application and server web-form.
My string using test database is: "jdbc:h2:~/test;AUTO_SERVER=TRUE"

Connecting to an Apache Derby/JavaDB database using Oracle SQL Developer [duplicate]

This question already has answers here:
How to use SQLDeveloper to connect to embedded Derby database
(4 answers)
Closed 5 years ago.
I've been trying to connect to an Apache Derby/JavaDB embedded database using Oracle SQL Developer but without much success.
I've tried to create a new connection using the following JDBC URL:
jdbc:derby:/path/to/file/database.derby;create=true
which resulted in an error:
Status : Failure -Test failed: Invalid connection info specified. Verify the URL format for the specified driver.
Previously I've added derby.jar through Tools > Preferences > Database > Third Party JDBC Drivers.
Given that JavaDB is now a supported Oracle product I'm not understanding why is not better integrated with its development tools.
Any guidance will be much appreciated. Thanks in advance.
Your derby url seems wrong. You need to point the url to the directory of the database, not the database itself.
jdbc:derby:/path/to/file/;create=true
Have a look at the examples.
jdbc:derby:/reference/phrases/french
Open a connection to the database /reference/phrases/french.
On a UNIX system, this would be the path of the directory. On a
Windows system, the path would be C:\reference\phrases\french if the
current drive were C. If a jar file storing databases were in the
user's classpath, this could also be a path within the jar file.
From the docs:
The connection URL syntax is as follows:
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]
Subsubprotocol, which is not typically specified, determines how Derby
looks for a database: in a directory, in memory, in a class path, or
in a jar file. Subsubprotocol is one of the following:
directory: The default. Specify this explicitly only to distinguish a database that might be ambiguous with one on the class
path.
try connecting to the Derby DB using the Eclipse Java EE IDE for Web Developers instead of Oracle SQL Developer. In the latest versions it is has the full integrations.

connecting JDBC

Hi guys:)
I am new to servlet.I dont know how to connect oracle database to the servlet application. Can anyone paste Oracle JDBC coding
Register Database Connection parameters:
To include your application specific connection parameters of your database, edit the file data-sources.xml under \config directory. Add the following lines to create a data source. Change the hostname, port, username/password, database name, driver type to suit your application. Make changes for url after # symbol. Save the file. This will register the data-source which can be used across your application.
<data-source
class="oracle.jdbc.pool.OracleConnectionPoolDataSource"
name="ifso817DS"
location="jdbc/Loneifso817DS"
xa-location="jdbc/xa/ifso817XADS"
ejb-location="jdbc/Pooledifso817DS"
url="jdbc:oracle:thin:#insn104a.idc.oracle.com:1521:ifso817"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="travel"
password="travel"
inactivity-timeout="30"
/>
That is taken from: http://www.oracle.com/technology/sample_code/tech/java/servlets/samples/TravelServlet/Readme.html
There are a lot of articles discussing this, but I'll give you the basic steps:
download Oracle JDBC driver from Oracle's homepage, it's called ojdbc6.jar if you're using Java 6
make sure your application can find it on its classpath
since you are using servlets, you're also using a Java application server; read relevant documentation about setting up a JNDI binding to your database (usually this binding will be called jdbc/connectionName)
inside of your application, use this code:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/connectionName");
Connection conn = ds.getConnection();
This should be enough to get you started.
For more information, Google is your friend. Check out Wikipedia's JDBC page. Google "Oracle JDBC connection" for more info.
One thing that might give you headaches if you've never done stuff like this: when defining Oracle's URL for your app. server, it's format is as follows:
jdbc:oracle:thin:[user/password]#[host][:port]:SID
So you have to substitute appropriate values when defining the connection for a JNDI binding.
check this http://www.java2s.com/Code/Java/Servlets/JDBCandServlet.htm. You need to modify driver class name and connection url to connect to Oracle DB. Check http://www.java2s.com/Code/Java/Database-SQL-JDBC/OracleJDBCDriverload.htm for Oracle DB connection details

Resources