首先,安裝MySQL,配置用戶名和密碼,建立數據庫。html
可參見以前的文章:java
http://www.cnblogs.com/mengdd/p/3154638.htmlmysql
http://www.cnblogs.com/mengdd/p/3154922.htmlsql
要用JDBC鏈接MySQL,須要一個驅動包:數據庫
能夠去這裏下載(其實完整安裝MySQL後能夠在安裝路徑中找到這個包):api
http://www.mysql.com/products/connector/oracle
解壓後將其中的mysql-connector-java-XXX-bin.jar文件加入到Web Project的lib中:yii
首先,須要加載驅動:jsp
// 加載驅動 Class.forName("com.mysql.jdbc.Driver");
而後,創建鏈接:ide
String dbUrl = "jdbc:mysql://localhost:3306/mydatabase1"; String dbUser = "your_username"; String dbPwd = "your_password"; // 打開數據庫鏈接 con = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
注意其中的端口號和數據庫名(mydatabase1)可能須要修改。
JDBC鏈接不一樣數據庫:
以後就能夠執行SQL語句了。
最後要記得關閉資源。
完整Helper類代碼(感謝ZJL同窗無私奉獻悉心指導):
package com.meng.utils; import java.sql.*; public class JDBCHelper { private Connection con = null; private PreparedStatement stmt = null; private ResultSet rs = null; public JDBCHelper() { try { // 加載驅動 Class.forName("com.mysql.jdbc.Driver"); String dbUrl = "jdbc:mysql://localhost:3306/mydatabase1"; String dbUser = "mengdd"; String dbPwd = "881103"; // 打開數據庫鏈接 con = DriverManager.getConnection(dbUrl, dbUser, dbPwd); } catch (ClassNotFoundException e) { System.out.println("Error: unable to load driver class!"); System.out .println("please check if you hava the mysql-connector jar file in your lib dir!"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet executeResultSet(String sql) { return executeResultSet(sql, null); } public ResultSet executeResultSet(String sql, Object[] params) { try { // 建立一個JDBC聲明 stmt = con.prepareStatement(sql); if (null != params && params.length != 0) { for (int i = 0; i < params.length; i++) { stmt.setObject(i + 1, params[i]); } } rs = stmt.executeQuery(); return rs; } catch (Exception ex) { throw new RuntimeException(ex.getMessage(), ex); } } public int executeUpdate(String sql) { return executeUpdate(sql, null); } public int executeUpdate(String sql, Object[] params) { try { // 建立一個JDBC聲明 stmt = con.prepareStatement(sql); if (null != params && params.length != 0) { for (int i = 0; i < params.length; i++) { stmt.setObject(i + 1, params[i]); } } return stmt.executeUpdate(); } catch (Exception ex) { throw new RuntimeException(ex.getMessage(), ex); } } public void close() { try { // 應該明確地關閉全部的數據庫資源 if (null != rs) rs.close(); if (null != stmt) stmt.close(); if (null != con) con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
測試代碼:
package com.meng.test; import java.sql.ResultSet; import java.sql.SQLException; import com.meng.utils.JDBCHelper; public class Test { public static void main(String[] args) { JDBCHelper dbHelper = new JDBCHelper(); ResultSet resultSet = dbHelper.executeResultSet("SELECT * FROM pet;"); try { while (resultSet.next()) { System.out.println("-------------"); System.out.println("name: " + resultSet.getString("name")); System.out.println("birth: " + resultSet.getString("birth")); } } catch (SQLException e) { e.printStackTrace(); } finally { dbHelper.close(); } } }
結果:
------------- name: Tutu birth: 2013-03-01 ------------- name: Whistler birth: 1997-12-09 ------------- name: WangWang birth: 2008-09-30 ------------- name: MiaoMiao birth: 2012-01-01
官方文檔:
Java SE Technologies – Database
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136101.html
JDBC Overview
http://www.oracle.com/technetwork/java/overview-141217.html
JDBC API
http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/
Tutorial JDBC basic
http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html
Getting Started:
http://docs.oracle.com/javase/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html
The JDBC classes are contained in the Java package java.sql and javax.sql
JDBC教程: