2011年4月5日 星期二

【轉貼】Accessing MySQL on NetBeans using JDBC, Part I: Create a connection

【轉貼】Accessing MySQL on NetBeans using JDBC, Part I: Create a connection

好吧 我比較智障
還是要有圖文解說我才看得懂
所以也順便轉貼一下

以下為原文




Step-by-Step guide

  1. Installation
    • Install NetBeans.
    • 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.)
    • 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.
      the library file
  2. Add JDBC Driver to the project on NetBeans (Add a library).
    Next, I create a new Java project on NetBeans named ‘TestMySQL’ and add ‘mysql-connector-java-5.0.6-bin.jar’ that I’ve just extracted from previous step to the project’s library.
    1. Create New Project called TestSQL.
      Create new NetBeans's project
    2. In Projects window, right click the project name and select Properties.
      Open project's properties
    3. Project Properties window appears. The Categories on left side, select Libraries. And on right side in Compile tab, click Add JAR/Folder.
      Add JAR file
    4. New Window appears, browse to the file ‘mysql-connector-java-5.0.6-bin.jar’ and click Open.
      Browse to the library file
    5. You’ll see the .jar file was added to the project. Click OK to finish.
      The library file was added
      Note: You should keep mysql-connector-java-5.0.6-bin.jar in the directory that you won’t delete it (ex. not in temp folder). May be in the same directory that keep common library files. If you delete the file without delete a link from the project, the project will show error about missing library.
  3. Connect to the database.
    Now I’m going to write some code to connect to MySQL database. I have configured MySQL service on localhost.
    1. I’m going to use Connection and DriverMapper Classes so I need to import libraries.
      import java.sql.*;
      Import classes to the project
    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.
      try {
                  Class.forName("com.mysql.jdbc.Driver");
                  String connectionUrl = "jdbc:mysql://localhost/mysql?" +
                                         "user=root&password=123456";
                  Connection con = DriverManager.getConnection(connectionUrl);
              } catch (SQLException e) {
                  System.out.println("SQL Exception: "+ e.toString());
              } catch (ClassNotFoundException cE) {
                  System.out.println("Class Not Found Exception: "+ cE.toString());
              }
      The code explanation:
      • Class.forName(“com.mysql.jdbc.Driver”); means load the MySQL driver.
      • “jdbc:mysql://localhost/mysql?” + “user=root&password=123456″; is a connection string that tells to connect MySQL on localhost, select database named ‘mysql’ and user/password for MySQL server.
        If you would like to connecto to other database, simply change text ‘mysql’ after ‘localhost/’ to your database name.
      Connection code
    3. Compile and run the project. If no error occurs, it means that the connection has established successfully.
      Connected MySQL successful
Next part, I’ll show to how to perform some basic SQL operations to MySQL.

沒有留言:

張貼留言