Sample code for connecting to MySQL database

Hi:

I am learning to use Eclipse.

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

Thanks,
Phil Miller
pillmillAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fargoCommented:
Mayank SAssociate Director - Product EngineeringCommented:
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
avinthmCommented:
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://www.developer.com/java/data/article.php/3417381

check out this link it has explanation in detail too.
ctamrakarCommented:
// 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);
    }
  }
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
ctamrakarCommented:
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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
I think this had to be a split.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.