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 {
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連線各種資料庫,程式碼都類似,不同之處僅在於資料庫連線字串。
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連線各種資料庫,程式碼都類似,不同之處僅在於資料庫連線字串。
相關文章
- JDBC連線各種資料庫的字串JDBC資料庫字串
- jdbc獲取各種資料庫連線JDBC資料庫
- JDBC連線資料庫JDBC資料庫
- java jdbc連線資料庫JavaJDBC資料庫
- JDBC連線各種資料庫的方法(經典)JDBC資料庫
- 【轉載】JDBC連線各種資料庫的字串JDBC資料庫字串
- jdbc獲取對各種資料庫的連線JDBC資料庫
- JDBC 連線oracle三種方法JDBCOracle
- JDBC、JDBC框架、資料庫事務、資料庫連線池JDBC框架資料庫
- 【JavaWeb】JDBC連線MySQL資料庫JavaWebJDBCMySql資料庫
- JDBC之連線sqlserver資料庫JDBCSQLServer資料庫
- JDBC連線資料庫步驟JDBC資料庫
- JDBC連線資料庫經驗JDBC資料庫
- JDBC 連線資料庫的類JDBC資料庫
- JDBC連線各資料庫大全JDBC資料庫
- jdbc獲取資料庫連線JDBC資料庫
- JAVA中十六種主流資料庫的JDBC連線字串Java資料庫JDBC字串
- Nodejs連線12種資料庫例子集合NodeJS資料庫
- java Jdbc連線oracle資料庫連線測試JavaJDBCOracle資料庫
- JDBC資料庫連線池實現JDBC資料庫
- 常用資料庫JDBC連線寫法資料庫JDBC
- JDBC---jsp資料庫連線JDBCJS資料庫
- jdbc-odbc連線資料庫 (轉)JDBC資料庫
- 各種資料庫的jdbc驅動下載及連線方式資料庫JDBC
- java應用通過jdbc連線資料庫jdbc的串的3種寫法:JavaJDBC資料庫
- JDBC連線批量處理資料入庫JDBC
- 淺談JDBC和資料庫連線池JDBC資料庫
- 常用資料庫JDBC連線寫法 (轉)資料庫JDBC
- asp.net連線Access資料庫例子ASP.NET資料庫
- 各種資料庫連線資料庫
- 資料庫連線的方法種種資料庫
- 各種連線資料庫的連線字串資料庫字串
- jdbc連線oracle rac資料庫的寫法JDBCOracle資料庫
- 資料庫連線分析(1)-從JDBC到MyBatis資料庫JDBCMyBatis
- JDBC【資料庫連線池、DbUtils框架、分頁】JDBC資料庫框架
- JDBC中連線資料庫的常用jar包JDBC資料庫JAR
- JDBC連線MySQL資料庫及演示樣例JDBCMySql資料庫
- [ 轉載]常用資料庫JDBC連線寫法資料庫JDBC