?
Solved

Sample code for connecting to MySQL database

Posted on 2006-04-24
8
Medium Priority
?
277 Views
Last Modified: 2010-03-31
Hi:

I am learning to use Eclipse.

Are there some sample projects available to
illustrate connecting to a SQL database ?

Thanks,
Phil Miller
0
Comment
Question by:pillmill
8 Comments
 
LVL 11

Expert Comment

by:fargo
ID: 16529174
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16531882
It doesn't matter whether you use Eclipse or not ;-)

See the JDBC tutorial: http://java.sun.com/docs/books/tutorial/

For MySQL, select the DB and download the driver from:

http://developers.sun.com/product/jdbc/drivers
0
 
LVL 6

Expert Comment

by:avinthm
ID: 16531899
Hi ,

You need to learn JDBC for this.
this link can help you to know abt JDBC
http://en.wikipedia.org/wiki/JDBC

some sample code....
http://java.sun.com/products/jdbc/reference/codesamples/index.html



cheers
0
Technology Partners: 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!

 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16532317
http://www.developer.com/java/data/article.php/3417381

check out this link it has explanation in detail too.
0
 
LVL 1

Expert Comment

by:ctamrakar
ID: 16678211
// consider mysqlConn is global variable and declared , you need a jdbc driver for mysql and put in classpath

Connection mysqlConn=null;

// sample method to connect to mysql

public void mySQLConnection() throws SQLException {
    if (mysqlConn == null) {
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      String url = "jdbc:mysql://" + ip-address + ":" + database_port + "/" + mysqldb_name;

      mysqlConn = DriverManager.getConnection(url, mysql_user, mysql_password);
      // set autocommit false
      mysqlConn.setAutoCommit(false);
    }
  }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16679628
>> DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Its better to use Class.forName () and provide the driver-name as a String (read it from a properties-file or a configuration-file) so that the code is independent of the driver/ DB being used. Even better to use a data-source.
0
 
LVL 1

Accepted Solution

by:
ctamrakar earned 500 total points
ID: 16679979
ye you could also get this info.
 String url = "jdbc:mysql://" + ip-address + ":" + database_port + "/" + mysqldb_name;
 
from a properties file or config files  so that its independent of driver/db



0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16766730
I think this had to be a split.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 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