Professional Web Applications Themes

Connecting to mysql via Java - MySQL

Hi, I am new to mysql. I need to connect to a mysql server from a Java application. Can someone share some sample code or point me to the right place for a sample? I am assuming that I'll have to download a Mysql driver and I'll be able to use the usual java.sql.* classes....

  1. #1

    Default Connecting to mysql via Java

    Hi, I am new to mysql. I need to connect to a mysql server from a Java
    application. Can someone share some sample code or point me to the
    right place for a sample?

    I am assuming that I'll have to download a Mysql driver and I'll be
    able to use the usual java.sql.* classes.

    yankeerivera@yahoo.com Guest

  2. #2

    Default Re: Connecting to mysql via Java

    com wrote: 

    You might find it helpful to read a couple of books:

    "Learning Java"

    http://www.oreilly.com/catalog/learnjava2/

    "Database Programming with JDBC & Java"

    http://www.oreilly.com/catalog/jdbc2/index.html

    The first book contains plenty of examples of Java techniques, such as
    opening a file and reading it a line at a time.

    The second book focusses on database programming with Java.

    You should then read the doentation about Connector/J, the JDBC
    library for MySQL:

    http://dev.mysql.com/doc/refman/4.1/en/connector-j.html

    This provides advice about how to obtain and install the latest version
    of Connector/J, and examples showing how to connect to MySQL and send
    commands and queries to the MySQL server.

    In a recent thread on this subject, Jeff Summers also recommended "MySQL
    and Java Developers Guide" by Matthews, Cole, and Gradecki, from Wiley.
    Mark Matthews is the principal developer of Connector/J, so you'll be
    learning from the expert.

    Good luck with your project.

    David Harper
    Cambridge, England
    David Guest

  3. #3

    Default Re: Connecting to mysql via Java

    com wrote: 

    That is correct you will need the Java Connector found at
    http://dev.mysql.com/downloads

    The basic flow for talking to MySQL via java is:

    1. Load the Driver
    2. Make the Connection
    3. Make the query
    4. Do something with results

    Sample/Starter code:

    public static boolean load_mysql_driver()
    {
    try {
    // The newInstance() call is a work around for some
    // broken Java implementations

    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    } catch (Exception ex) {
    System.err.println(ex);// handle the error
    return false;
    }
    return true;
    }

    public static Connection connect_mysql(String hostname, String
    database, String username, String password)
    {
    Connection conn;
    try {

    conn = DriverManager.getConnection("jdbc:mysql://" + hostname +
    "/"+ database +"?user="+username+"&password=" + password);

    } catch (SQLException ex) {
    // handle any errors
    System.err.println("SQLException: " + ex.getMessage());
    System.err.println("SQLState: " + ex.getSQLState());
    System.err.println("VendorError: " + ex.getErrorCode());
    return null;
    }

    return conn;
    }

    public static void main (String args[])
    {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    if(load_mysql_driver())
    {
    System.out.println("Unable to load MySQL Driver!");
    return;
    }

    conn = connect_mysql("hostname", "database", "username", "password");

    try
    {
    stmt = conn.createStatement();
    rs = stmt.executeQuery( "SELECT * FROM myTable t" );

    while(rs.next())
    {
    //Do something with the results
    }
    }
    catch(SQLException e)
    {
    System.err.println(e);
    return;
    }
    }


    That's the very basics. If you google for MySQL java examples you
    should find a bunch.

    Dr.Zoidberb Guest

  4. #4

    Default Re: Connecting to mysql via Java

    com wrote in news:1165255854.409987.82500
    f1g2000cwa.googlegroups.com:
     

    Hi,

    All the steps you need to take to connect to a MySQL database, and some
    sample code can be found here:
    http://jcsnippets.atspace.com/java/database/connect-to-mysql-database.html

    Best regards,

    JayCee
    --
    http://jcsnippets.atspace.com/
    a collection of source code, tips and tricks
    jcsnippets.atspace.com Guest

Similar Threads

  1. Connecting to a java servlet
    By amitsa in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: August 19th, 10:32 AM
  2. Connecting with MySQL
    By Ashley_02 in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: July 15th, 02:47 AM
  3. Connecting to FCS from Java
    By GMina in forum Macromedia Flash Flashcom
    Replies: 1
    Last Post: March 30th, 02:10 PM
  4. connecting mysql
    By dummmmmbo in forum Dreamweaver AppDev
    Replies: 1
    Last Post: March 1st, 12:04 AM
  5. Can director support a java applet connecting to a database
    By furbyagogo webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 3
    Last Post: September 9th, 02:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139