Solved

Connecting to MYSQL

Posted on 2002-04-19
11
235 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 143
Java Jpanels and Jframe 8 28
eclipse argument 14 53
rhino JavaScript import, load 25 66
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

914 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

21 Experts available now in Live!

Get 1:1 Help Now