Connect Mysql using Java
Connect MySQL with Java (NetBeans)
In this article i will explain as clearly as possible how to connect to MySQL using Java in Netbeans, I will start from the idea that you already have a project set up and what you need is to connect to the database, I will also assume that you have knowledge basics of MySQL and Java.
However, you may find some of the links below helpful before you begin:
NetBeans IDE 6.0.1: http://download.netbeans.org/netbeans/6.0/final/
MySQL 5.0: http://dev.mysql.com/downloads/mysql/5.0.html
If you run on Windows I recommend using a very simple tool to manage MySQL called easyphp… for more information refer to:
http://www.easyphp.org/index.php,
for download: http://downloads.sourceforge.net/quickeasyphp/EasyPHP-2.0b1 -setup.exe?modtime=1166851545&big_mirror=0
If you use Linux you may want to use Xampp, similar to easyphp… for more information: http://www.apachefriends.org/en/xampp-linux.html,
for download:
http ://www.apachefriends.org/download.php?xampp-linux-1.6.6.tar.gz
MySQL Connector Java: http://dev.mysql.com/downloads/connector/j/5.1.html Java Development
Kit : https://sdlc2e.sun.com/ECom/EComActionServlet;jsessionid=F988B2338AEE708AA38D059375BDFB87
Once everything is ready (Netbeans and MySQL installed on our computer) we proceed to copy the file mysql-connector-java-5.1.5-bin.jar in the following directories:
In my case:
/home/jimenez/jdk1.6.0_03/jre/lib/ext /home/jimenez/jdk1.6.0_03/bin
If it is on another computer, it might be something like this: C:Program FilesJavajdk1.6.0 _01/jre/lib/ext
C:Program FilesJavajdk1.6.0_01/bin
The point is that you must find the directory where you have the JDK installed and from there the directories indicated above. This is important so that Netbeans can connect to the database and no error in “classForName=’com.mysql.jdbc.Driver’ Not Found”.
Once this is done, we properly enter the connection with Java. First, the JDBC driver must be imported using the instruction: classForName=”com.mysql.jdbc.Driver”; Then establish the connection to the JDBC using a URL connection string: url = “jdbc:mysql://localhost:3306/Database_Name”; In addition, the username and password for the connection must be established: login = “root”; password = “”; Finally, the Connection object that will be used to communicate with the database through the application, it provides methods to handle transaction processing, to create objects and execute SQL statements and stored procedures. Connection connection; Very good! After a quick review of the matter, we finally created the following class in our project with the name of DataBase.java Note: For this example I use a database called “University”, I have also installed MySQL with the default configuration, if for For example, you modified the port, replace the URL string where it says “3306” with the corresponding port, I have not set any password either, so I have left the password string empty, basically if you installed with the preset options you will not have any problem. import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class DataBase { public static Connection connection; public static String login = “root”; public static String password = “”; public static String url = “jdbc:mysql://localhost:3306/ University”; public static String classForName=”com.mysql.jdbc.Driver”;
public static void Connect() { //connect to the database try{ Class.forName(DataBase.classForName).newInstance(); connection = DriverManager.getConnection(DataBase.url,DataBase.login,DataBase.password); } catch (InstantiationException ex) { Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex); }catch(SQLException ex){ System.out.println(ex.getMessage()); }catch(ClassNotFoundException ex){ System.out.println(ex.getMessage()); } } public static void Disconnect(){ //disconnects from the database try { connection.close(); } catch (SQLException ex) { Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex); } } }
Very good! At this point the thing should already work, however we haven’t added any records yet! You must first connect to the database: DataBase.Connect(); then create a Statement object that handles the SQL statement: Statement statement = DataBase.connection.createStatement(); consequently, we execute the object: statement.executeUpdate(“INSERT INTO students(card,name) VALUES(‘A77662′,’Mario’)”); we finish it: statement.close(); and disconnect from the database:
DataBase.Disconnect(); I will briefly explain the following methods of the Statement object execute( ) :