Accessing MySQL On NetBeans Using JDBC
Accessing MySQL On NetBeans Using JDBC
a connection
Java, NetBeans
Introduction
This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J,
MySQL AB’s JDBC Driver for MySQL.
from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to
a database.
Requirements
Step-by-Step guide
1. Installation
o Install NetBeans.
o Download MySQL Connector/J, name ‘mysql-connector-java-5.0.6.zip’. (The
file name may differs depends on the version if you’ve downloaded from the
Official Site at here.)
o Extract the zip file to a folder, you’ll see file ‘mysql-connector-java-5.0.6-bin.jar’
which is the library file that we want. Just copy the file to the library folder, for
example to “C:\Program Files\Java\jdk1.6.0_02\lib” directory.
2. I’ll connect to MySQL Server on local machine, the mysql database(a default
database in MySQL). In main method, add the following code.
3. try {
4. Class.forName("com.mysql.jdbc.Driver");
5. String connectionUrl = "jdbc:mysql://localhost/mysql?"
+
6. "user=root&password=123456";
7. Connection con =
DriverManager.getConnection(connectionUrl);
8. } catch (SQLException e) {
9. System.out.println("SQL Exception: "+ e.toString());
10. } catch (ClassNotFoundException cE) {
11. System.out.println("Class Not Found Exception: "+
cE.toString());
}