Sajad
Sajad

Reputation: 2363

JDBC MySQl don't show result sets

In my code, My finally output, is not like in MySQl console output:

public class d3 {

Connection con;
String dbName = "mydb";
String dbUsername = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/";

public d3() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Find database successfuly");
    } catch (Exception e) {
        System.err.println("Unable to find and load driver");
        System.exit(1);
    }
}

public void connectToDB() {
    try {
        con = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
        System.out.println("Connect to database succesfully");
    } catch (SQLException e) {
        System.out.println("Can not connect to database");
        System.exit(1);
    }
}

public void excuteSQL() {
    String sqlString1 = "use mydb";
    String sqlString2 = "show tables";
    String result;
    try {
        Statement st1 = con.createStatement();

        ResultSet result1 = st1.executeQuery(sqlString1);
         while(result1.next()){
         result = result1.getString(dbName);
         System.out.println(result1);
        }
    } catch (SQLException sqle) {
        System.out.println("Can not excute sql statement");
    }
}

public static void main(String[] args) {
    d3 ddd = new d3();
    ddd.connectToDB();
    ddd.excuteSQL();
}
}

My output:

Find database successfuly
Connect to database succesfully
Result set representing update count of 0

But, I want to be like this:

Find database successfuly
Connect to database succesfully
Database changed      // this is shown in console

I am going step by step and comparing outputs from mysql console and my IDE console, For example, I want to see "Database changed" message after execute "use mydb" statement.

Upvotes: 0

Views: 1959

Answers (2)

Mark Rotteveel
Mark Rotteveel

Reputation: 108971

You are executing use mydb, which doesn't produce a result set (it isn't a query). Also, you should not use this command to set your current database, JDBC requires you to use the JDBC methods. See the javadoc of java.sql.Connection:

Note: When configuring a Connection, JDBC applications should use the appropriate Connection method such as setAutoCommit or setTransactionIsolation. Applications should not invoke SQL commands directly to change the connection's configuration when there is a JDBC method available.

The equivalent of USE mydb is Connection.setCatalog("mydb"), calling this method makes sure the driver is in a consistent state. If you look at the actual implementation in MySQL Connector/J this method eventually calls USE mydb, but it also does additional checks and changes to the state of the Connection object.

When you already know which database you want to use, just include it directly into the connection URL:

String dbUrl = "jdbc:mysql://localhost/mydb";

Similarly, you shouldn't use SHOW TABLES to get information on tables, use DatabaseMetaData.getTables() instead. Using JDBC methods over MySQL specific commands makes your program more portable.

Upvotes: 1

Karki64
Karki64

Reputation: 23

you should do:

while (resultset.next()) {

System.out.println(resultset.getString("somevariable")
}

Some variable just represents what you are looking for in your table. Also, I'm not sure that "use mydb" works with JDBC. If you use a command string like

("SELECT NAME FROM TABLE")

then you would have to use something like

System.out.println(resultset.getString("NAME")

to look for stuff in the table column named "NAME".

Upvotes: 1

Related Questions