Sample code for connecting to MySQL database


I am learning to use Eclipse.

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

Phil Miller
Who is Participating?

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

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.

Mayank SAssociate Director - Product EngineeringCommented:
It doesn't matter whether you use Eclipse or not ;-)

See the JDBC tutorial:

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

You need to learn JDBC for this.
this link can help you to know abt JDBC

some sample code....

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:

check out this link it has explanation in detail too.
// 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
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.
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

From novice to tech pro — start learning today.