This post originated from an RSS feed registered with Java Buzz
by Javin Paul.
Original Post: How to connect MySQL database from Java program with Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
When you start learning JDBC in Java, first program you want to execute
is connect to database from Java and get some result back by executing some SELECT
queries. In this Java program we will learn How to connect to MySQL database from Java program and execute
query against it. I choose MySQL database because its free and and easy to
install and setup. If you are using Netbeans IDE than you can connect MySQL
Server directly from Netbeans, Which means in one window you could be writing
Java code and other you can write SQL queries. Another advantage of using MySQL
database is that it provides type
4 JDBC driver bundled in : mysql-connector-java-5.1.17-bin.jar which is
easy to use. By the way if you are using Oracle database than you can check Java
program to connect Oracle database, to connect and run SQL queries against
Oracle db. This Java tutorial also explains some of the common error which
comes while working in JDBC code e.g. during connection or reading results. In
order to connect to MySQL database you need three things database URL, username
and password and we are using default user root here, which is created during
MySQL installation. By the way we have also used PreparedStatement
for connection because it’s one of the JDBC
best practice to use PreparedStatement for better performance and avoiding
SQL Injection.