• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Connecting to MYSQL

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
rahul_ashish
Asked:
rahul_ashish
  • 6
  • 2
  • 2
  • +1
1 Solution
 
coreyitCommented:
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
 
jose_tijoCommented:
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
 
heyhey_Commented:
> do i need a separate driver to connect to MySQL from java

yes - you need to download MySQL JDBC driver
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
rahul_ashishAuthor Commented:
For Jose


Did u download any 8.jar file for that except one from SourceForge.net
0
 
rahul_ashishAuthor Commented:
For Jose


Did u download any *.jar file for that except one from SourceForge.net
0
 
rahul_ashishAuthor Commented:
Thanks for the help
0
 
coreyitCommented:
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
 
jose_tijoCommented:
Hi

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

Tijo Jose
0
 
rahul_ashishAuthor Commented:
hey what is the port number for connecting to the MySQL?
0
 
rahul_ashishAuthor Commented:
hey what is the port number for connecting to the MySQL?
0
 
rahul_ashishAuthor Commented:
hey what is the port number for connecting to the MySQL?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now