Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

java and mysql

Thread view: 
zbiszko - 15 Nov 2007 22:59 GMT
Hello

how can I get all databeses from mysql database?
I was trying such code, but without any results:(

Connection con;
 con = getConnection("jdbc:mysql://localhost:3306", "root",
"pasword");
 Statement st = (Statement) con.createStatement();
 ResultSet rs = st.executeQuery("Show databases;");

Thanks for any hepl
Zbiszko
Thomas Kellerer - 15 Nov 2007 23:16 GMT
zbiszko wrote on 15.11.2007 23:59:
> Hello
>
[quoted text clipped - 6 lines]
>   Statement st = (Statement) con.createStatement();
>   ResultSet rs = st.executeQuery("Show databases;");

Try DatabaseMetaData.getCatalogs()

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/DatabaseMetaData.html#getCatalogs()

Btw: when using executeQuery() the statement may not be terminated with a
semicolon. So probably running st.executeQuery("show databases"); would work as
well.

Thomas
Martin Gregorie - 15 Nov 2007 23:45 GMT
> Hello
>
[quoted text clipped - 6 lines]
>   Statement st = (Statement) con.createStatement();
>   ResultSet rs = st.executeQuery("Show databases;");

Now you've got the list in your result set, you need to read through it
a row at a time:

    while (rs.next())
    {
       /* Retrieve and print the columns in this row */
    }

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Arne Vajhøj - 17 Nov 2007 04:40 GMT
> how can I get all databeses from mysql database?
> I was trying such code, but without any results:(
[quoted text clipped - 4 lines]
>   Statement st = (Statement) con.createStatement();
>   ResultSet rs = st.executeQuery("Show databases;");

Code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ShowDatabases {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/Test", "root", "");
        Statement stmt = con.createStatement();
        ResultSet rs1 = stmt.executeQuery("SHOW DATABASES");
        while(rs1.next()) {
            System.out.println(rs1.getString(1));
        }
        rs1.close();
        ResultSet rs2 = stmt.executeQuery("SELECT SCHEMA_NAME FROM
INFORMATION_SCHEMA.SCHEMATA");
        while(rs2.next()) {
            System.out.println(rs2.getString(1));
        }
        rs2.close();
        ResultSet rs3 = con.getMetaData().getCatalogs();
        while(rs3.next()) {
            System.out.println(rs3.getString(1));
        }
        rs3.close();
        con.close();
    }
}

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.