DbConnection

packagedbconnection;

//importjava.io.*;

//importjava.awt.List;

importjava.sql.*;

//importjava.util.ArrayList;

importjava.util.Collection;

importjava.util.Iterator;

importjava.util.Map;

importjava.util.Set;

//importjavax.sql.*;

importjava.util.*;

//importjavax.naming.*;

importjava.util.HashMap;

/*格式一:OracleJDBCThinusingaServiceNamejdbc:oracle:thin:@//<host>:<port>/<service_name>

*Example:jdbc:oracle:thin:@//192.168.2.1:1521/xifenfei

*格式二:OracleJDBCThinusinganSIDjdbc:oracle:thin:@<host>:<port>:<SID>

*Example:jdbc:oracle:thin:192.168.2.1:1521:xff--注意这里的格式,@后面有//,这是与使用SID的主要区别。

*格式三:OracleJDBCThinusingaTNSNamejdbc:oracle:thin:@<TNSName>

*Example:jdbc:oracle:thin:@GL

*--SupportforTNSNameswasaddedinthedriverrelease10.2.0.1

*/

publicclassDbConnection{

publicstaticvoidmain(String[]args)throwsClassNotFoundException,SQLException

{

System.out.println("test");

//連接數據庫的驅動

Stringdriver="oracle.jdbc.driver.OracleDriver";

//連接數據的具體信息,如IP地址接口,SID

Stringurl="jdbc:oracle:thin:@172.24.22.1:1521:g2shjdb";

//Stringurl="jdbc:oracle:thin:@//172.24.22.1:1521/TESTS10";

Class.forName(driver);

Objectfactno=null,brandno=null,bname=null,mark=null;

//連接數據庫

Connectionconn=DriverManager.getConnection(url,"nbmis","nbmis");

//取消自動提交功能

conn.setAutoCommit(false);

//設置連接數據庫的sql,?表示需傳入參數

PreparedStatementpst=conn.prepareStatement("select*frombrandweiwherefact_no=?andbrand_no=?");

//設定第一個參數值

pst.setString(1,"0236");

//設定第二個參數值

pst.setString(2,"NB");

//真正執行查詢語句,并把結果放入ResultSet游標中

ResultSetreader=pst.executeQuery();

while(reader.next())

{

//Stringbname=(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掉數據庫中的資料

PreparedStatementpst2=conn.prepareStatement("deletefrombrandweiwherefact_no=?andbrand_no=?");

pst2.setString(1,"0236");

pst2.setString(2,"NB");

//返回刪除了多少行,如沒有刪除到則返回0

intdelcount=pst2.executeUpdate();

System.out.println("deletecount="+delcount);

System.out.println("----------------------------");

//update資料到數據庫中

PreparedStatementpst3=conn.prepareStatement("updatebrandweisetmark_no='W'wherefact_no=?andbrand_no=?");

pst3.setString(1,"0236");

pst3.setString(2,"WR");

intupdcount=pst3.executeUpdate();

System.out.println("updatecount="+updcount);

System.out.println("----------------------------");

//insertinto資料到數據庫中

PreparedStatementpst4=conn.prepareStatement("insertintobrandwei(fact_no,brand_no,brand_nm,mark_no)values('0236',?,?,?)");

pst4.setString(1,"NB");

pst4.setString(2,"NEWBALANCE");

pst4.setString(3,"test");

intintcount=pst4.executeUpdate();

System.out.println("insertintocount="+intcount);

System.out.println("----------------------------");

//設置連接數據庫的sql,?表示需傳入參數

PreparedStatementpst8=conn.prepareStatement("select*frombrandweiwherefact_no=?");

//設定第一個參數值

pst8.setString(1,"0236");

//真正執行查詢語句,并把結果放入ResultSet游標中

ResultSetreader8=pst8.executeQuery();

while(reader8.next())

{

//Stringbname=(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=newHashMap<String,Object>();

/*向集合中添加指定的键值对*/

hm.put("11","testmap1!");

hm.put("22","testmap222222!");

//遍歷方法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());

}

Listlist=newArrayList();

ArrayListarrayList=newArrayList();

Lista=newArrayList();

//list.trimToSize();//错误,没有该方法。ArrayList特有的方法

list.clear();

arrayList.trimToSize();//ArrayList里有该方法。

List<Integer>s=newArrayList<Integer>();

s.add(600);

s.add(100);

s.add(200);

//使用for-each

for(intlta:s)

{

System.out.println(lta);

}

/*ArrayList<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();

HashMap<String,String>map=newHashMap<String,String>();

map.put("1","CSDN");

list.add(map);*/

//關閉游標

reader8.close();

//disconnection斷掉與數據庫的連接

conn.close();

}

}

相关推荐