Solved

Connecting to MYSQL

Posted on 2002-04-19
11
237 Views
Last Modified: 2010-03-31
HI
I have a database in MySQL but i don't know know how to connect to MySQL. i want know, do i need a separate driver to connect to MySQL from java or the java ODBC will connect me to MySQL. if i need a separate driver then from where can i get that.
0
Comment
Question by:rahul_ashish
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 2

Expert Comment

by:coreyit
ID: 6954596
A good mysql driver is available at:
  http://mmmysql.sourceforge.net/

A sample connection:
  Connection conn = null;
  try {
    DriverManager.registerDriver( new org.gjt.mm.mysql.Driver() );
    conn = DriverManager.getConnection( "jdbc:mysql://" + DBserverName + ":" + DBport + "/" + DBName + "?user=" + DBuser + "&password=" + DBpassword );
  }
  catch( SQLException e ) {
    System.out.println( "argh!" );
  }

-corey
0
 
LVL 1

Accepted Solution

by:
jose_tijo earned 50 total points
ID: 6955794
import java.util.*;
import java.sql.*;
import org.gjt.mm.mysql.*;

public class BnmySQLConnect {
  private java.sql.Connection connection = null;
  private org.gjt.mm.mysql.Driver myDriver = null;
  private java.sql.Statement stmt = null;
  private java.sql.ResultSet result = null;
  private java.util.Vector vtrValues = new Vector();

  public java.sql.Connection getConnection() {
    try {
      Properties props = new Properties();
      props.put("user","root");
      props.put("password", "mypass");
      myDriver = new org.gjt.mm.mysql.Driver();
      System.out.println("Driver: " + myDriver);
      connection = myDriver.connect("jdbc:mysql://127.0.0.1:<portnumber>/<dbname>", props);
      return connection;
    }catch (Exception e) {
      System.out.println(e);
    }
    return connection;
  }

  public boolean closeConnection() {
    try {
      connection.close();
      return true;
    }catch (Exception e) {
      System.out.println(e);
      return false;
     }
  }
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6956061
> do i need a separate driver to connect to MySQL from java

yes - you need to download MySQL JDBC driver
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:rahul_ashish
ID: 6956793
For Jose


Did u download any 8.jar file for that except one from SourceForge.net
0
 
LVL 1

Author Comment

by:rahul_ashish
ID: 6956794
For Jose


Did u download any *.jar file for that except one from SourceForge.net
0
 
LVL 1

Author Comment

by:rahul_ashish
ID: 6956797
Thanks for the help
0
 
LVL 2

Expert Comment

by:coreyit
ID: 6956964
I'm curious about a couple things:
1. did my suggestion not work?
2. since jose_tijo answered your question thoroughly, why does he only receive a "b"?

-corey
0
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6958817
Hi

  I downloaded only the driver from sourceforge.net. Nothing else.

Tijo Jose
0
 
LVL 1

Author Comment

by:rahul_ashish
ID: 6982626
hey what is the port number for connecting to the MySQL?
0
 
LVL 1

Author Comment

by:rahul_ashish
ID: 6982640
hey what is the port number for connecting to the MySQL?
0
 
LVL 1

Author Comment

by:rahul_ashish
ID: 6982684
hey what is the port number for connecting to the MySQL?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
tomcat not starting 6 68
jdbc error in jsp application 20 63
Which non-HTML GUI front end to use with Java? 3 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question