JDBC连接三种数据库例子
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 {