List:MySQL and Java« Previous MessageNext Message »
From:<mmatthew Date:March 28 1999 2:54pm
Subject:Re: mm jdbc on win95
View as plain text  
On Sat, 27 Mar 1999, Eric Eldred wrote:

> 
> /**
> *thanks for the hints!  just in case anyone feels like
> *developing a faq, this ought to be added.  code follows
> and the experts here can tune it up better than me:
> /*
> 
> /** Simple - test mysql connection with mm jdbc driver
> * @args - none
> * jdk1.17b on win95 with mysql - won't work under jdk1.2 with mm
> driver
> * install org/gjt/mm/mysql/* under jdk home
> * set CLASSPATH=
> */
> import org.gjt.mm.mysql.*;
> //also import following packages separately:
> import org.gjt.mm.mysql.Connection;
> //if you use a Statement in your code add this:
> //import org.gjt.mm.mysql.Statement;
> import org.gjt.mm.mysql.Driver;

Do not import anything from org.gjt.mm.mysql or you will have problems
later on. You never need to import any classes for any JDBC driver.

> import java.sql.*;
> // in case you use the Properties part, commented out here:
> //import java.util.Properties;
> 
> public class Simple
> {
> static public void main(String args[])
> {
> // this Connection is from java.sql.Connection
>                 Connection conn = null;
>         try
> // use .newInstance(); per the README file
>         {      
> Class.forName("org.gjt.mm.mysql.Driver").newInstance();
>         }
>         catch (Exception e)
>         {
>                 System.out.println("driver failure");
>                 e.printStackTrace();
>                 return;
>         }
> // following may be needed for some applets(?) -- see mm's README
> /*      Properties P = System.getProperties();
>         P.put("jdbc.drivers", "org.gjt.mm.mysql.Driver");
>         System.setProperties(P);
> */

The above won't work for applets, only applications (see my docs). Applets
don't have to capability of setting system properties.

>         try
> /** NOTE: use the "?user=" syntax instead of another string arg to
> getConnection
> * use the semicolon before 'password' instead of the '&', at least
> for win32
> * you MUST supply the user and password strings -- don't leave out
> * use 3306 as port, though it may be default
> * 'test' is the database name
> * first set up 'monty' or whoever with mysql privileges, username,
> and password and
> * be sure to do a 'mysqladmin reload' afterwards - see mysql manual
> */

You can use any of the getConnection() methods from DriverManager that you
wish. They all work. You don't have to specify the port unless it is
different than 3306. As far as ";" vs "&" it is not a platform thing, it
is a version thing. Anything earlier than 1.1c (which you shouldn't be
using anymore, because 1.1i is out) uses ";" for a parameter separator,
where anything after 1.1c uses "&" for a parameter separator, to be more
in line with HTTP urls. 

>         {       String url =
> "jdbc:mysql://localhost:3306/test/?user=monty;password=''";    
> // don't forget to cast with (Connection) back to
> java.sql.Connection type
>                 conn = (Connection)
> DriverManager.getConnection(url);
>                 System.out.println("Connection successful");
>         }
>         catch (SQLException e)
>         {       System.out.println("Connection failed");
>                 e.printStackTrace();
>         }
>         finally
>         {       try
>                 {       conn.close();
>                         System.out.println("Connection closed");
>                 }
>                 catch (Exception e)
>                 {       System.out.println("Connection exception");
>                         e.printStackTrace();
>                 }
>         }
> }
> }

Ok, if you didn't import org.gjt.mm.mysql.Connection, then you wouldn't
have to do all of this casting. Don't import anything from
org.gjt.mm.mysql, and only cast Statements to org.gjt.mm.mysql.Statement
if you need to use the non-JDBC methods like getLastInsertID(). 

	-Mark
--
Mark Matthews <mmatthew@stripped>
http://www.ccm.ecn.purdue.edu/~mmatthew/
"Computers in the future may weigh no more than 2 tons." -Pop.Mech., 1947

Thread
Re: mm jdbc on win95Eric Eldred28 Mar
  • Re: mm jdbc on win95mmatthew28 Mar