JDBC連線三種資料庫例子

metrowilliam發表於2011-11-15
1、JDBC連線SQL Server 2005資料庫
package com.gui.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 連線SQL Server資料庫
*
* @author 鄔厚建
*/
public class ConnSQL {
public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=ibatis";
public static final String USERNAME = "sa";
public static final String PASSWORD = "sa2005";

/**
* 獲得資料庫連線物件
*
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}// 載入驅動
catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static void main(String[] args) {
Connection connection = getConnection();
String sql = "select * from t_user";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet res = ps.executeQuery();
while (res.next()) {
int id = res.getInt("id");
String name = res.getString("name");
String sex = res.getInt("sex") == 1 ? "男" : "女";
System.out.println("id:" + id + "===name:" + name + "===sex:"
+ sex);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

2、JDBC連線Oracle資料庫
package com.gui.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 連線Oracle資料庫
*
* @author 鄔厚建
*
*/
public class ConnOracle {
public static String Driver = "oracle.jdbc.driver.OracleDriver";
public static String Url = "jdbc:oracle:thin:@localhost:1521:xe";
public static String username = "study";
public static String password = "oracle";

public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(Driver);// 載入驅動
connection = DriverManager.getConnection(Url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static void main(String[] args) {
Connection connection = getConnection();
String sql = "select * from dept";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet res = ps.executeQuery();
while (res.next()) {
System.out.println("deptNo:" + res.getInt("deptNo") + "==="
+ "deptName:" + res.getString("deptName"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

3、JDBC連線MySQL資料庫
package com.gui.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 連線MySQL資料庫
*
* @author 鄔厚建
*
*/
public class ConnMySQL {
public static final String Driver = "org.gjt.mm.mysql.Driver";
public static final String Url = "jdbc:mysql://localhost:3306/wuhoujian";
public static final String Username = "root";
public static final String Password = "1";

/**
* 獲得連線物件
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(Driver);
connection = DriverManager.getConnection(Url, Username, Password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static void main(String[] args) {
String sqlText = "select * from testext";
Connection connection = getConnection();
try {
PreparedStatement ps = connection.prepareStatement(sqlText);
ResultSet res = ps.executeQuery();
while (res.next()) {
System.out.println("id:" + res.getInt("id") + "===" + "name:"
+ res.getString("name") + "===" + "age:"
+ res.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

注意:在使用JDBC連線資料庫的時候,專案中一定要新增相關驅動包!縱觀上面的程式碼,JDBC連線各種資料庫,程式碼都類似,不同之處僅在於資料庫連線字串。

相關文章