Solved

Connecting to MYSQL

Posted on 2002-04-19
11
234 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now