Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:42:40+00:00 2026-06-14T11:42:40+00:00

I have a Java program connection to a MySQL database, how can I change

  • 0

I have a Java program connection to a MySQL database, how can I change the current database to a different one on the same connection?

I connect to MySQL like this:

DriverManager.getConnection("jdbc:mysql://"+server+"/",log,pass);

After some operations I want to connect to a different mysql database on the same connection. How can I do that?

I tried to use:

Statement stat= con.createStatement();
ResultSet r=stat.executeQuery("use mysql"); 

But that does not change the database to be used.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:42:42+00:00Added an answer on June 14, 2026 at 11:42 am

    As described in the MySQL documentation you need to use Connection.setCatalog() to switch to another database. It also explicitly says that you should not execute a USE <databasename> to switch.

    The reason for this warning is that JDBC is a generic interface to databases and therefor provides methods for most common tasks, including switching catalogs (or databases as they are in MySQL). The JDBC specification/javadoc also explicitly says that people should use the API over database specific commands (if both are available). There are several reasons for this: 1) it promotes database-independent code, and 2) the driver might do additional things internally in response to one of the API methods. Using database specific commands might cause the driver to misbehave because its internal state does not match the database state.

    A call to setCatalog(String) will not affect existing statements, as specified in the JDBC API documentation:

    Calling setCatalog has no effect on previously created or prepared Statement objects. It is implementation defined whether a DBMS prepare operation takes place immediately when the Connection method prepareStatement or prepareCall is invoked. For maximum portability, setCatalog should be called before a Statement is created or prepared.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program which does CRUD on a MySQL database. For this,
I have a java program in that I am using mysql database connectivity code.
I have a java program that performs 5 different tasks. When I run the
I have a java program that is running in the background(Windows). I would like
I have a Java program with Maven managing its dependencies. One of those dependency
How to access the another system mysql database through java program?Am using the following
I have a Java program that mirrors a connection from a client server to
I have a Java program that opens a socket connection to a server that
I have a problem with my Java program. It has a socket connection between
I'm having problems with creating mySQL table within Java program. I constantly get Can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.