File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to specify the name of the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to specify the name of the database" Watch "How to specify the name of the database" New topic
Author

How to specify the name of the database

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
RESOLVED i HAD TO ENTER THE URL FOR THE CONNECTION POOL IN TWO PLACES IN THE GLASSFISH ADMIN CONSOLE.

I created a connection pool to a MySQL database in GlassFish. I did specify the database, but executing SQL statements I am getting the error "No database selected". I remember reading that you can specify the name of the database in JDBC, but I have not been able to find any setter or method in Connection or Statement.


Your help will be greatly appreciated,
Alejandro Barrero
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29220
    
135

It's part of the Connection string URL. This is specified in the datasource if you are using one or driver manager if you are not.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
Thank you Jeanne, but I have the database name in the URL "jdbc:mysql://localhost:3306/MySQ". I remember that there is a method in some JDBC class to specify the name of the database; it is something like "use", but i don't remember the details.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29220
    
135

"use databaseName" is a SQL command. "set databaseName" is similar but for the first time. I've never seen either used in JDBC before though I suppose you could try passing them via a statement and see what happens. My guess is they won't work because you aren't yet connected to a database and JDBC doesn't have the concept of connecting to a database server without the database.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
Thanks again. What I am doing is

I know that there is a connection. The line "ResultSet result = statement.executeQuery(selectStatement);" gets executed and throws an exception with the error "No database selected".
I remember reading that this is a problem with MySQL and the solution was to somehow set the database. Unfortunately, "statement.execute("USE MySQL");" is giving me errors. This is happening in a MySQL connection pool and somebody knows the answer.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2476
    
    7

What is the connection string in jdbc/paperles? Does it contain a database name?

Is MySQ your database name?


OCUP UML fundamental and ITIL foundation
youtube channel
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
Thank you Jan. I access the database in DbVisualiser with"jdbc:mysql://localhost:3306/MySQL". DbVisualiser shows "MySQL" as the root and node "mysql" contains the tables. In the GlassFish administrator console I created the connection pool with "DatabaseName" as "mysql" and I used "jdbc:mysql://localhost:3306/MySQL" as "url". "jdbc/paperles" is the name of the JDBC Resource in GlassFish.
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

Hope the connection retrieveal is done at a different location. Try to use the connection object which is retrieved , instead of calling this.datasource.getConnection();

Because, hope this datasource object is not getting initialized. Just keep and sop statement before "conn = this.dataSource.getConnection();" to check whether this.datasource is initialized or not.

If possible give us the complete code , if the code is restrictive to the company, provide the sample code how you might have programmed this part.

Alejandro Barrero wrote:Thanks again. What I am doing is

I know that there is a connection. The line "ResultSet result = statement.executeQuery(selectStatement);" gets executed and throws an exception with the error "No database selected".
I remember reading that this is a problem with MySQL and the solution was to somehow set the database. Unfortunately, "statement.execute("USE MySQL");" is giving me errors. This is happening in a MySQL connection pool and somebody knows the answer.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
Thanks for your help Smarty.
What I have is a war file with a servlet installed in GlassFish. I cannot debug the code in the servlet; all I can do is print messages to response.getWriter() and get them running the servlet. The application is a personal application and I would be glad to provide the code for the entire servlet but it is somewhat long and I am giving here the code that I believe is relevant. I was creating the DataSource in the init() method, but following your suggestion, for testing, I am using

For testing, I am now using

In a test program i have

The test program is printing:
TESTING method=TEST
SQLException No database selected


Since the SQLException happens in "statement.executeQuery" I think it means that "statement.executeQuery" is executed as a method in object "statement" and that the connection and the DataSource with the connection pool exist. As I said before, I did set the database name and url (jdbc:mysql://localhost:3306/MySQL) parameters when creating the connection pool in GlassFish. It appears that for some strange reason in the interaction between MySQL and GlassFish the database is not set. I remember reading some time ago that this could happen and that I had to explicitly set the database name, but I don't remember how.
For further testing I changed the driver form mysql-connector-java-5.1.16-bin.jar to mysql-connector-java-5.1.2.jar but i am still getting the same error.
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59




As per your exception , In above code the exception has not occurred. If it is so then it should not occur in the below code also



Because here you are making use of the same datasource to retrieve the connection. If you are able to retrieve the connection in init then you must able to retrieve the connection in the above code.

Alejandro , check whether did you declared another local variable datasource in init() method also along with class member variable datasource.

Also make the following change in your code..





Also try to provide the full stacktrace. you can get that from console or from server log. I dont know exactly where it would be in your glassfish server.

Mean while i will also look on this point on internet.

Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2476
    
    7

Alejandro Barrero wrote:Thank you Jan. I access the database in DbVisualiser with"jdbc:mysql://localhost:3306/MySQL". DbVisualiser shows "MySQL" as the root and node "mysql" contains the tables. In the GlassFish administrator console I created the connection pool with "DatabaseName" as "mysql" and I used "jdbc:mysql://localhost:3306/MySQL" as "url". "jdbc/paperles" is the name of the JDBC Resource in GlassFish.
But you use MySQ in stead of MySQL:
but I have the database name in the URL "jdbc:mysql://localhost:3306/MySQ"
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2476
    
    7

Alejandro, please do not alter the first post in the thread after it has been going on for a while. It becomes difficult to follow.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 302
I will keep that in mind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to specify the name of the database
 
Similar Threads
Connectio pool: Oracle and Glassfishv3
Problem Using DataSource and Creating Connection Pool
EJB & JPA Unexpected Results
Glassfish Connection Pooling
toplink.jdbc.url ignored in persistence.xml