Solved

Connecting to MYSQL

Posted on 2002-04-19
11
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: 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.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

717 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