help in connecting database.

Hi,
I have got a problem in connecting database to java(JDBC). I have downloaded Java_2 how to programme 6th edition from net. But i dont have the CD to install the java driver and Mysql language. Please help me by giving the website addresses of those menstioned above.

Comments

  • : Hi,
    : I have got a problem in connecting database to java(JDBC). I have downloaded Java_2 how to programme 6th edition from net. But i dont have the CD to install the java driver and Mysql language. Please help me by giving the website addresses of those menstioned above.
    :
    Java and JDBC can be downloaded from http://java.sun.com MySQL from http://dev.mysql.com/ . Other information can be found at http://www.google.com
  • : : Hi,
    : : I have got a problem in connecting database to java(JDBC). I have downloaded Java_2 how to programme 6th edition from net. But i dont have the CD to install the java driver and Mysql language. Please help me by giving the website addresses of those menstioned above.
    : :
    : Java and JDBC can be downloaded from http://java.sun.com MySQL from http://dev.mysql.com/ . Other information can be found at http://www.google.com
    :
    Hi tnx for replying me.
    I have downloaded mysqlConnector and mysql from those websites mensioned by u. But the thing is i'm not able to connect to the database url. I kept that connector in the same root directory(as in the jdbc program's root directory). My program is like this.

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

    public class DisplayAuthors
    {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DATABASE_URL = "jdbc:mysql://localhost/books";

    // launch the application
    public static void main( String args[] )
    {
    Connection connection = null; // manages connection
    Statement statement = null; // query statement

    // connect to database books and query database
    try
    {
    Class.forName( JDBC_DRIVER ); // load database driver class

    // establish connection to database
    connection =
    DriverManager.getConnection( DATABASE_URL, "jhtp6", "jhtp6" );

    // create Statement for querying database
    statement = connection.createStatement();

    // query database
    ResultSet resultSet = statement.executeQuery(
    "SELECT authorID, firstName, lastName FROM authors" );

    // process query results
    ResultSetMetaData metaData = resultSet.getMetaData();
    int numberOfColumns = metaData.getColumnCount();
    System.out.println( "Authors Table of Books Database:" );

    for ( int i = 1; i <= numberOfColumns; i++ )
    System.out.printf( "%-8s ", metaData.getColumnName( i ) );
    System.out.println();

    while ( resultSet.next() )
    {
    for ( int i = 1; i <= numberOfColumns; i++ )
    System.out.printf( "%-8s ", resultSet.getObject( i ) );
    System.out.println();
    } // end while
    } // end try
    catch ( SQLException sqlException )
    {
    sqlException.printStackTrace();
    System.exit( 1 );
    } // end catch
    catch ( ClassNotFoundException classNotFound )
    {
    classNotFound.printStackTrace();
    System.exit( 1 );
    } // end catch
    finally // ensure statement and connection are closed properly
    {
    try
    {
    statement.close();
    connection.close();
    } // end try
    catch ( Exception exception )
    {
    exception.printStackTrace();
    System.exit( 1 );
    } // end catch
    } // end finally
    } // end main
    } // end class DisplayAuthors


    Please help me in connecting database.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories