Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Connecting to MYSQL

Posted on 2002-04-19
11
Medium Priority
?
244 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 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

618 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