課程
/數(shù)據(jù)庫
/Oracle
/Oracle數(shù)據(jù)庫開發(fā)必備利器之SQL基礎
java怎樣連接數(shù)據(jù)庫?
2015-12-15
源自:Oracle數(shù)據(jù)庫開發(fā)必備利器之SQL基礎 2-6
正在回答
個人研究用: 0.去oracle安裝目錄找一個名為class12.jar的jar包,放到項目的lib里 1.在META-INF?建立個context.xml文件 <?xml?version="1.0"?encoding="UTF-8"?> <Context> ??<Resource?name="jdbc/crm"?? ???????auth="Container"?????? ???????type="javax.sql.DataSource"?????? ???????driverClassName="oracle.jdbc.driver.OracleDriver"?????? ???????url="jdbc:oracle:thin:@localhost:1521:oracle"?????? ???????username="crm"?????? ???????password="crm"?????? ???????maxActive="4"?????? ???????maxIdle="2"?????? ???????maxWait="3000"/>? </Context> 2. //數(shù)據(jù)庫鏈接池?第一個類?用來鏈接數(shù)據(jù)庫 package?com.baidu.crm.common; import?java.sql.Connection; import?java.sql.ResultSet; import?java.sql.SQLException; import?java.sql.Statement; import?javax.naming.Context; import?javax.naming.InitialContext; import?javax.naming.NamingException; import?javax.sql.DataSource; import?org.omg.CORBA.Request; //連接池 public?class?DataSourceManager?{ public?static?Connection?getConnection(){ DataSource?ds=null; try?{ Context?context=new?InitialContext(); ds=(DataSource)context.lookup("java:comp/env/jdbc/crm");//crm數(shù)據(jù)庫名 }?catch?(NamingException?e)?{ e.printStackTrace(); } Connection?con=null; try?{ con=ds.getConnection(); }?catch?(SQLException?e)?{ e.printStackTrace(); } return?con; } public?static?void?closeDBSource(ResultSet?rs,Statement?st,Connection?con){ try?{ if(rs!=null){ rs.close(); } if(st!=null){ st.close(); } if(con!=null){ con.close(); } }?catch?(SQLException?e)?{ e.printStackTrace(); } } } 3.//調用鏈接的類 package?com.baidu.crm.dao; import?java.sql.Connection; import?java.sql.PreparedStatement; import?java.sql.ResultSet; import?java.sql.SQLException; import?java.util.ArrayList; import?java.util.List; import?com.baidu.crm.common.DataSourceManager; import?com.baidu.crm.entity.City; public?class?CityDao?{ public?List<City>?getCityList() { List<City>?result=new?ArrayList<City>(); Connection?con=null; PreparedStatement?ps=null; ResultSet?rs=null; con=DataSourceManager.getConnection(); try?{ ps=con.prepareStatement("select?city_code,city_name?from?city"); rs=ps.executeQuery(); while(rs.next()){ City?city=new?City(); city.setCityCode(rs.getInt("city_code")); city.setCityName(rs.getString("city_name")); result.add(city); } }?catch?(SQLException?e)?{ e.printStackTrace(); }finally{ DataSourceManager.closeDBSource(rs,?ps,?con); } return?result; } }
你可以往后面看,學jdbc就會了!慕課網(wǎng)有這門課
舉報
為你帶來Oracle開發(fā)必備的sql基礎,為后續(xù)課程學習打下好的基礎
2 回答求幫助,很急
4 回答Oracle卸載問題,解決不了,請求幫忙,急,謝謝!!
3 回答數(shù)據(jù)庫連接不上
1 回答使用shutdown immediated 后無法連接數(shù)據(jù)庫
2 回答我想卸載Oracle,但是一直都不行,求大神幫忙。。。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2015-12-15
2015-12-15
你可以往后面看,學jdbc就會了!慕課網(wǎng)有這門課