DbConnection
戚哲
2023-12-01
package dbconnection;
//import java.io.*;
//import java.awt.List;
import java.sql.*;
//import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//import javax.sql.*;
import java.util.*;
//import javax.naming.*;
import java.util.HashMap;
/*格式一: Oracle JDBC Thin using a ServiceName jdbc:oracle:thin:@//<host>:<port>/<service_name>
* Example: jdbc:oracle:thin:@//192.168.2.1:1521/xifenfei
* 格式二: Oracle JDBC Thin using an SID jdbc:oracle:thin:@<host>:<port>:<SID>
* Example: jdbc:oracle:thin:192.168.2.1:1521:xff --注意这里的格式,@后面有//, 这是与使用SID的主要区别。
* 格式三:Oracle JDBC Thin using a TNSName jdbc:oracle:thin:@<TNSName>
* Example: jdbc:oracle:thin:@GL
* --Support for TNSNames was added in the driver release 10.2.0.1
*/
public class DbConnection {
public static void main(String [] args) throws ClassNotFoundException, SQLException
{
System.out.println("test");
//連接數據庫的驅動
String driver = "oracle.jdbc.driver.OracleDriver";
//連接數據的具體信息,如IP地址接口,SID
String url="jdbc:oracle:thin:@172.24.22.1:1521:g2shjdb";
//String url="jdbc:oracle:thin:@//172.24.22.1:1521/TESTS10";
Class.forName(driver);
Object factno = null,brandno = null,bname = null,mark = null;
//連接數據庫
Connection conn=DriverManager.getConnection(url,"nbmis","nbmis");
//取消自動提交功能
conn.setAutoCommit(false);
//設置連接數據庫的sql,? 表示需傳入參數
PreparedStatement pst = conn.prepareStatement("select * from brandwei where fact_no =? and brand_no =?");
//設定第一個參數值
pst.setString(1, "0236");
//設定第二個參數值
pst.setString(2, "NB");
//真正執行查詢語句,并把結果放入ResultSet游標中
ResultSet reader = pst.executeQuery();
while(reader.next())
{
//String bname = (String) ResultSet.getString(1);
factno = reader.getObject(1);
brandno = reader.getObject(2);
bname = reader.getObject(3);
mark = reader.getObject(4);
System.out.print(factno);
System.out.print(' ');
System.out.print(brandno);
System.out.print(' ');
System.out.print(bname);
System.out.print(' ');
System.out.println(mark);
}
//關閉游標
reader.close();
System.out.println("----------------------------");
//delete 掉數據庫中的資料
PreparedStatement pst2 = conn.prepareStatement("delete from brandwei where fact_no =? and brand_no =?");
pst2.setString(1, "0236");
pst2.setString(2, "NB");
//返回刪除了多少行,如沒有刪除到則返回0
int delcount = pst2.executeUpdate();
System.out.println("delete count=" + delcount);
System.out.println("----------------------------");
//update資料到數據庫中
PreparedStatement pst3 = conn.prepareStatement("update brandwei set mark_no='W' where fact_no =? and brand_no =?");
pst3.setString(1, "0236");
pst3.setString(2, "WR");
int updcount = pst3.executeUpdate();
System.out.println("update count=" + updcount);
System.out.println("----------------------------");
//insert into 資料到數據庫中
PreparedStatement pst4 = conn.prepareStatement("insert into brandwei(fact_no, brand_no, brand_nm, mark_no) values('0236', ?, ?, ?)");
pst4.setString(1, "NB");
pst4.setString(2, "NEW BALANCE");
pst4.setString(3, "test");
int intcount = pst4.executeUpdate();
System.out.println("insert into count=" + intcount);
System.out.println("----------------------------");
//設置連接數據庫的sql,? 表示需傳入參數
PreparedStatement pst8 = conn.prepareStatement("select * from brandwei where fact_no =?");
//設定第一個參數值
pst8.setString(1, "0236");
//真正執行查詢語句,并把結果放入ResultSet游標中
ResultSet reader8 = pst8.executeQuery();
while(reader8.next())
{
//String bname = (String) ResultSet.getString(1);
factno = reader8.getObject(1);
brandno = reader8.getObject(2);
bname = reader8.getObject(3);
mark = reader8.getObject(4);
System.out.print(factno);
System.out.print(' ');
System.out.print(brandno);
System.out.print(' ');
System.out.print(bname);
System.out.print(' ');
System.out.println(mark);
}
HashMap<String,Object> hm = new HashMap<String,Object>();
/*向集合中添加指定的键值对*/
hm.put("11", "test map1!");
hm.put("22", "test map222222!");
//遍歷方法1
Iterator<String> iterator = hm.keySet().iterator();
while(iterator.hasNext()) {
System.out.println(hm.get(iterator.next()));
}
//遍歷方法2
Collection<Object> c = hm.values();
for(Iterator<Object> it=c.iterator();it.hasNext();){
System.out.println(it.next());
}
//遍歷方法3
Set<Map.Entry<String, Object>> set=hm.entrySet();
for(Iterator<Map.Entry<String, Object>> it=set.iterator();it.hasNext();){
Map.Entry<String, Object> mapEnter=it.next();
System.out.println("key="+mapEnter.getKey()+",value="+mapEnter.getValue());
}
List list = new ArrayList();
ArrayList arrayList = new ArrayList();
List a=new ArrayList();
//list.trimToSize(); //错误,没有该方法。ArrayList特有的方法
list.clear();
arrayList.trimToSize(); //ArrayList里有该方法。
List<Integer> s = new ArrayList<Integer>();
s.add(600);
s.add(100);
s.add(200);
//使用for-each
for(int lta:s)
{
System.out.println(lta);
}
/*ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("1","CSDN");
list.add(map);*/
//關閉游標
reader8.close();
//disconnection斷掉與數據庫的連接
conn.close();
}
}