1.連接MySQL數據庫
package mysql; import java.sql.*; /** * @author xys */ public class ConnectMysql { public static Connection getConnection() throws ClassNotFoundException, SQLException { String url = "jdbc:mysql://localhost:3306/databaseName"; String user = "mysqluser"; String password = "password"; String driverClass = "com.mysql.cj.jdbc.Driver"; Connection connection = null; Class.forName(driverClass); try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } if (connection != null) { System.out.println("數據庫連接成功"); } else { System.out.println("數據庫連接失敗"); connection.close(); } return connection; } public void getResult() throws ClassNotFoundException, SQLException { // 實例化 Statement 對象 Statement statement = getConnection().createStatement(); // 要執行的 Mysql 數據庫操作語句(增、刪、改、查) String sql = ""; // 展開結果集數據庫 ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()) { // 通過字段檢索 int id = resultSet.getInt("id"); String name = resultSet.getString("name"); // 輸出數據 System.out.println("ID : " +id); System.out.println("name :" + name); } // 完成后需要依次關閉 resultSet.close(); statement.close(); getConnection().close(); } }2.連接Oracle數據庫
package cn.zhisheng.test.jdbc; import oracle.jdbc.driver.OracleDriver; import java.sql.*; import java.util.Properties; /** * Created by 10412 on 2016/12/27. * JDBC的六大步驟 * JAVA連接Oracle的三種方式 */ public class JdbcTest { public static void main(String[] args) { Connection connect = null; Statement statement = null; ResultSet resultSet = null; try { //第一步:注冊驅動 //第一種方式:類加載(常用) //Class.forName("oracle.jdbc.OracleDriver"); //第二種方式:利用Driver對象 Driver driver = new OracleDriver(); DriverManager.deregisterDriver(driver); //第二步:獲取連接 //第一種方式:利用DriverManager(常用) //connect = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "你的oracle數據庫用戶名", "用戶名密碼"); //第二種方式:直接使用Driver Properties pro = new Properties(); pro.put("user", "你的oracle數據庫用戶名"); pro.put("password", "用戶名密碼"); connect = driver.connect("jdbc:oracle:thin:@localhost:1521:XE", pro); //測試connect正確與否 System.out.println(connect); //第三步:獲取執行sql語句對象 //第一種方式:statement //statement = connect.createStatement(); //第二種方式:PreStatement PreparedStatement preState = connect.prepareStatement("select * from tb1_dept where id = ?"); //第四步:執行sql語句 //第一種方式: //resultSet = statement.executeQuery("select * from tb1_dept"); //第二種方式: preState.setInt(1, 2);//1是指sql語句中第一個?, 2是指第一個?的values值 //resultSet = preState.executeQuery(); //執行查詢語句 //查詢任何語句,如果有結果集,返回true,沒有的話返回false,注意如果是插入一條數據的話,雖然是沒有結果集,返回false,但是卻能成功的插入一條數據 boolean execute = preState.execute(); System.out.println(execute); //第五步:處理結果集 while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String city = resultSet.getString("city"); System.out.println(id+" "+name+" "+city); //打印輸出結果集 } } catch (Exception e) { e.printStackTrace(); }finally { //第六步:關閉資源 try { if (resultSet!=null) resultSet.close(); if (statement!=null) statement.close(); if (connect!=null) connect.close(); } catch (SQLException e) { e.printStackTrace(); } } } }3.連接SqlServer數據庫
package com.connDBTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class GetConnectionSqlServer { public void getConnectionSqlServer() { String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost:1433;databasename=USCSecondhandMarketDB"; // 1433是端口,"USCSecondhandMarketDB"是數據庫名稱 String userName = "sa"; // 用戶名 String userPwd = "sa"; // 密碼 Connection dbConn = null; try { Class.forName(driverName).newInstance(); } catch (Exception ex) { System.out.println("驅動加載失敗"); ex.printStackTrace(); } try { dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("成功連接數據庫!"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (dbConn != null) dbConn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { GetConnectionSqlServer getConn = new GetConnectionSqlServer(); getConn.getConnectionSqlServer(); } }
推薦文章