import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; public class Test { public static void main(String[] args) { // testPreparedStatement(); // testStatement(); testProperties(); testUpdate(); } public static Properties getProperties() { Properties prop = new Properties(); try { // /加載屬性列表 //InputStream in = new BufferedInputStream(new FileInputStream( //"D:\\myselenium\\config.properties")); //讀取當前工程下的配置文件 InputStream in =Test.class.getResourceAsStream("/properties/config.properties"); //當配置文件粘貼到工程中,與類文件同級或放於一個文件夾內時,加載配置文件得用當前類名.class prop.load(in); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void testUpdate() { Properties prop = getProperties(); Connection con = null;// 建立一個數據庫鏈接 // 建立預編譯語句對象,通常都是用這個而不用Statement--PreparedStatement表示預編譯的 SQL 語句的對象。 Statement statement = null; ResultSet resultset=null; try { Class.forName(prop.getProperty("orcal.driverName"));// 加載Oracle驅動程序--返回與帶有給定字符串名的類或接口相關聯的 Class 對象 System.out.println("開始嘗試鏈接數據庫!"); String url = prop.getProperty("PA18dburl");// 127.0.0.1是本機地址,XE是精簡版Oracle的默認數據庫名 String user = prop.getProperty("PA18dbuser");// 用戶名,系統默認的帳戶名 String password = prop.getProperty("PA18sdbpsw");// 你安裝時選設置的密碼 con = DriverManager.getConnection(url, user, password);// 獲取鏈接--DriverManager類管理一組 JDBC 驅動程序的基本服務 System.out.println("鏈接成功!"); String sql1="update t_accepted_customer_info i set custname='王賽333' where i.userid='10517733'"; String sql = "select * from t_accepted_customer_info i where i.userid='10517733'"; statement=con.createStatement(); System.out.println(sql1); int count = statement.executeUpdate(sql1);//增刪改都用這個方法 System.out.println("count="+count); if(count>0){ System.out.println("success"); }else{ System.out.println("失敗"); } resultset= statement.executeQuery(sql);//查詢SQL並保存到結果集 while (resultset.next()){ System.out.println("學號:" + resultset.getInt("id") + "姓名:"+ resultset.getString("custname")+"idno:"+resultset.getString("idno")); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一將上面的幾個對象關閉,由於不關閉的話會影響性能、而且佔用資源 // 注意關閉的順序,最後使用的最早關閉 if (statement != null) statement.close(); if (statement != null) statement.close(); if (con != null) con.close(); System.out.println("數據庫鏈接已關閉!"); } catch (Exception e) { e.printStackTrace(); } } } public static void testProperties() { Properties prop = getProperties(); Connection con = null;// 建立一個數據庫鏈接 PreparedStatement pre = null;// 建立預編譯語句對象,通常都是用這個而不用Statement--PreparedStatement表示預編譯的 SQL 語句的對象。 ResultSet result = null;// 建立一個結果集對象 try { Class.forName(prop.getProperty("orcal.driverName"));// 加載Oracle驅動程序 System.out.println("開始嘗試鏈接數據庫!"); String url = prop.getProperty("PA18dburl");// 127.0.0.1是本機地址,XE是精簡版Oracle的默認數據庫名 String user = prop.getProperty("PA18dbuser");// 用戶名,系統默認的帳戶名 String password = prop.getProperty("PA18sdbpsw");// 你安裝時選設置的密碼 con = DriverManager.getConnection(url, user, password);// 獲取鏈接--DriverManager類管理一組 JDBC 驅動程序的基本服務 System.out.println("鏈接成功!"); String sql = "select * from t_accepted_customer_info i where i.custname=? and i.userid= ?";// 預編譯語句,「?」表明參數 pre = con.prepareStatement(sql);// 實例化預編譯語句--建立一個 PreparedStatement 對象來將參數化的 SQL 語句發送到數據庫 pre.setString(1, "王賽");// 設置參數,前面的1表示參數的索引,而不是表中列名的索引 pre.setString(2, "10517733");// 設置參數,前面的1表示參數的索引,而不是表中列名的索引 result = pre.executeQuery();// 執行查詢,注意括號中不須要再加參數-- 在此 PreparedStatement 對象中執行 SQL 查詢,並返回該查詢生成的 ResultSet 對象。 while (result.next()) // 當結果集不爲空時 System.out.println("學號:" + result.getInt("id") + "姓名:" + result.getString("custname")); } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一將上面的幾個對象關閉,由於不關閉的話會影響性能、而且佔用資源 // 注意關閉的順序,最後使用的最早關閉 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("數據庫鏈接已關閉!"); } catch (Exception e) { e.printStackTrace(); } } } public static void testPreparedStatement() { Connection con = null;// 建立一個數據庫鏈接 PreparedStatement pre = null;// 建立預編譯語句對象,通常都是用這個而不用Statement ResultSet result = null;// 建立一個結果集對象 try { Class.forName("oracle.jdbc.driver.OracleDriver");// 加載Oracle驅動程序 System.out.println("開始嘗試鏈接數據庫!"); String url = "jdbc:oracle:" + "thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.167.101)(PORT = 1522))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.167.102)(PORT = 1522))(LOAD_BALANCE = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = fatpa18)(FAILOVER_MODE = (TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5))))";// 127.0.0.1是本機地址,XE是精簡版Oracle的默認數據庫名 String user = "fat_dev";// 用戶名,系統默認的帳戶名 String password = "paic1234";// 你安裝時選設置的密碼 con = DriverManager.getConnection(url, user, password);// 獲取鏈接 System.out.println("鏈接成功!"); String sql = "select * from t_accepted_customer_info i where i.custname=? and i.userid= ?";// 預編譯語句,「?」表明參數 pre = con.prepareStatement(sql);// 實例化預編譯語句 pre.setString(1, "王賽");// 設置參數,前面的1表示參數的索引,而不是表中列名的索引 pre.setString(2, "21884");// 設置參數,前面的1表示參數的索引,而不是表中列名的索引 result = pre.executeQuery();// 執行查詢,注意括號中不須要再加參數 while (result.next()) // 當結果集不爲空時 System.out.println("學號:" + result.getInt("id") + "姓名:" + result.getString("custname")); } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一將上面的幾個對象關閉,由於不關閉的話會影響性能、而且佔用資源 // 注意關閉的順序,最後使用的最早關閉 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("數據庫鏈接已關閉!"); } catch (Exception e) { e.printStackTrace(); } } } public static void testStatement() { Connection con = null;// 建立一個數據庫鏈接 Statement statement = null;// 建立預編譯語句對象,通常都是用這個而不用Statement ResultSet result = null;// 建立一個結果集對象 try { String custName = "王賽"; Class.forName("oracle.jdbc.driver.OracleDriver");// 加載Oracle驅動程序 System.out.println("開始嘗試鏈接數據庫!"); String url = "jdbc:oracle:" + "thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.167.101)(PORT = 1522))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.167.102)(PORT = 1522))(LOAD_BALANCE = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = fatpa18)(FAILOVER_MODE = (TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5))))";// 127.0.0.1是本機地址,XE是精簡版Oracle的默認數據庫名 String user = "fat_dev";// 用戶名,系統默認的帳戶名 String password = "paic1234";// 你安裝時選設置的密碼 con = DriverManager.getConnection(url, user, password);// 獲取鏈接 System.out.println("鏈接成功!"); statement = con.createStatement(); String sql = "select * from t_accepted_customer_info i where i.custname='" + custName + "'";// 預編譯語句,「?」表明參數 result = statement.executeQuery(sql);// 執行查詢,注意括號中不須要再加參數 while (result.next()) // 當結果集不爲空時 System.out.println("學號:" + result.getInt("id") + "姓名:" + result.getString("custname")); } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一將上面的幾個對象關閉,由於不關閉的話會影響性能、而且佔用資源 // 注意關閉的順序,最後使用的最早關閉 if (result != null) result.close(); if (statement != null) statement.close(); if (con != null) con.close(); System.out.println("數據庫鏈接已關閉!"); } catch (Exception e) { e.printStackTrace(); } } } }
如下是整個工程的圖片,包括配置文件(可創建一個FOLDER文件,把配置文件粘貼到文件夾中);html
若粘貼配置文件到工程中,則須要指定配置文件的存放路徑,即//InputStream in = new BufferedInputStream(new FileInputStream( //"D:\\myselenium\\config.properties"));,如java
1、關於配置文件的建立以下:sql
一、先新建一個文本文檔,修改後綴成.properties,這樣就能夠叫配置文件了數據庫
二、config.properties配置文件的編寫內容相似以下:編程
orcal.driverName=oracle.jdbc.driver.OracleDriveroracle
#PA18-UAT
PA18dburl=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = spa181.dbstg.paic.com.cn)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST =spa182.dbstg.paic.com.cn)(PORT = 1521))(LOAD_BALANCE = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = hsspa18)(FAILOVER_MODE = (TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5))))
PA18dbuser=sisopr
PA18sdbpsw=pasc_stg_pa18性能
三、以上配置文件中的#號表明「註釋」url
2、下載java API幫助文檔,查看 一些類或接口的方法及使用,可以儘快掌握編程方法,達到目的spa
3、鏈接數據庫,必須下載ojdbc6-11.1.0.6.jar,而後添加JAR包到工程中(可先建個LIBS,把文件粘貼,而後右擊添加),才能加載數據庫驅動code
4、自動化調用配置文件及鏈接後臺數據庫查詢的編寫思路:一、加載Oracle驅動程序;二、初始化鏈接串、用戶名和密碼,創建鏈接,鏈接數據庫;三、鏈接數據庫後,初始化SQL語名;四、對SQL語句發送到數據庫進行編譯;五、執行SQL,保存到結果集resultSet中;六、用.next()方法判斷結果集,如不爲空就經過遊標一一輸出 相應的結果;七、關閉鏈接等
注:JAVA中的異常處理,捕捉最大範圍的異常纔不會報錯,異常範圍過小會報異常,通常用Exception e
如下是本身寫的一個查詢數據庫例子
package seleniumtest; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import java.sql.Connection; public class getPropertes { public static void main(String[] args) { testQuery(); } public static Properties getPRO() { Properties pro = new Properties(); pro.setProperty("PA18UAT", "11111111111111"); InputStream in = getPropertes.class.getResourceAsStream("config.properties"); try { pro.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return pro; } public static void testQuery() Properties pro = getPRO(); Connection conn = null; Statement statement = null; ResultSet result = null; try { Class.forName(pro.getProperty("orcal.driverName")); // 建立鏈接 String url = pro.getProperty("PA18dburl"); String user = pro.getProperty("PA18dbuser"); String pwd = pro.getProperty("PA18sdbpsw"); conn = DriverManager.getConnection(url, user, pwd); statement = conn.createStatement(); String sql = "select * from t_accepted_customer_info i where i.userid='10517733'"; result = statement.executeQuery(sql); while (result.next()) { System.out.println("id:" + result.getInt("id") + "\t" + "custname:" + result.getString("custname")); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (result != null) { result.close(); } if (conn != null) { conn.close(); } if (statement != null) { statement.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
如下是另外寫的一個簡單打印出配置文件中的全部鍵與值 的代碼:
public class PropertyTest { public static void main(String[] args) { Properties prop = new Properties(); try {// 讀取屬性文件config.properties--用輸入流類把配置文件讀取出來 //BufferedInputStream(InputStream in) 建立一個 BufferedInputStream 並保存其參數,即輸入流 in,以便未來使用,想當於建立一個緩衝區。 //InputStream in = new BufferedInputStream(new FileInputStream( //"D:\\myselenium\\config.properties")); //InputStream in = ClassLoader.class.getResourceAsStream("/properties/config.properties");//讀取SRC下的配置文件 InputStream in=new FileInputStream("D:\\myselenium\\config.properties"); prop.setProperty("後管UAT", "http://soasadmin-stg.paic.com.cn/admin/admin/login.html"); prop.load(in); // /加載屬性列表 Iterator<String> it = prop.stringPropertyNames().iterator();//stringPropertyNames方法返回一個Set鍵集,iterator()返回一個迭代元素的迭代器 while (it.hasNext()) {//若是仍有元素能夠迭代,則返回 true String key = it.next();//返回迭代的下一個元素 System.out.println(key + ":" + prop.getProperty(key));//獲取指定鍵的屬性值 } in.close(); // /保存屬性到b.properties文件 // FileOutputStream oFile = new FileOutputStream("config.properties", true);//true表示追加打開 //prop.setProperty("phone", "10086"); //prop.store(oFile, "The New properties file"); // oFile.close(); } catch (Exception e) { System.out.println(e); } }}