;
帖子
3919 
精华
积分
11710 
鱼币
0 ¥ 
来自
大连 
在线时间
1858 小时 
注册时间
2007-6-11 
最后登录
2009-10-20 

贴图大师勋章新人进步勋章宣传大使勋章论坛元老勋章

3
发表于 2009-1-5 19:48 | 只看该作者
Servlet涓璲dbc搴旂敤楂樼骇绡囷紙涓锛 - 缂栫▼搴 - Powered by SupeSite

浣犵殑浣嶇疆锛缂栫▼搴 >> 璧勮 >> JAVA棰戦亾 >> SERVLET&JSP >> 璇︾粏鍐呭 鍦ㄧ嚎鎶曠ǹ

Servlet涓璲dbc搴旂敤楂樼骇绡囷紙涓锛

鍙戝竷: 2008-12-03 23:08 |  浣滆: 缃戠粶杞浇 |   鏉ユ簮: 缃戠粶杞浇 |  鏌ョ湅: 18娆

銆銆JDBC浣跨敤鏁版嵁搴揢RL鏉ヨ鏄庢暟鎹簱椹卞姩绋嬪簭銆傛暟鎹簱URL绫讳技浜庨氱敤鐨刄RL锛屼絾SUN 鍦ㄥ畾涔夋椂浣滀簡涓鐐圭畝鍖栵紝鍏惰娉曞涓嬶細

Jdbc::[node]/[database]

銆銆鍏朵腑瀛愬崗璁紙subprotocal锛夊畾涔夐┍鍔ㄧ▼搴忕被鍨嬶紝node鎻愪緵缃戠粶鏁版嵁搴撶殑浣嶇疆鍜岀鍙e彿锛屽悗闈㈣窡鍙夌殑鍙傛暟銆備緥濡傦細

String url=鈥漥dbc:inetdae:myserver:1433?language=us-english&sql7=true鈥

銆銆琛ㄧず閲囩敤inetdae椹卞姩绋嬪簭杩炴帴1433绔彛涓婄殑myserver鏁版嵁搴撴湇鍔″櫒锛岄夋嫨璇█涓虹編鍥借嫳璇紝鏁版嵁搴撶殑鐗堟湰鏄痬ssql server 7.0銆

銆銆java搴旂敤閫氳繃鎸囧畾DriverManager瑁呭叆涓涓┍鍔ㄧ▼搴忕被銆傝娉曞涓嬶細

Class.forName(鈥鈥);



Class.forName(鈥鈥).newInstance();

銆銆鐒跺悗锛孌riverManager鍒涘缓涓涓壒瀹氱殑杩炴帴锛

Connection connection=DriverManager.getConnection(url,login,password)锛

銆銆Connection鎺ュ彛閫氳繃鎸囧畾鏁版嵁搴撲綅缃紝鐧诲綍鍚嶅拰瀵嗙爜杩炴帴鏁版嵁搴撱侰onnection鎺ュ彛鍒涘缓涓涓猄tatement瀹

渚嬫墽琛岄渶瑕佺殑鏌ヨ锛

Statement stmt=connection.createStatement();

銆銆Statement鍏锋湁鍚勭鏂规硶锛圓PI锛夛紝濡俥xecuteQuery锛宔xecute绛夊彲浠ヨ繑鍥炴煡璇㈢殑缁撴灉闆嗐傜粨鏋滈泦鏄竴涓猂esultSet瀵硅薄銆傚叿浣撶殑鍙互閫氳繃jdbc寮鍙戞枃妗f煡鐪嬨傚彲浠un鐨勭珯鐐逛笂涓嬭浇

涓嬮潰渚嬪瓙鏉ヨ鏄庯細

import java.sql.*; // 杈撳叆JDBC package

String url = "jdbc:inetdae:myserver:1433";// 涓绘満鍚嶅拰绔彛

String login = "user";// 鐧诲綍鍚

String password = "";// 瀵嗙爜

try {

銆銆DriverManager.setLogStream(System.out); file://涓烘樉绀轰竴浜涚殑淇℃伅鎵撳紑涓涓祦

銆銆file://璋冪敤椹卞姩绋嬪簭锛屽叾鍚嶅瓧涓篶om.inet.tds.TdsDriver

銆銆file://Class.forName("com.inet.tds.TdsDriver")锛

銆銆file://璁剧疆瓒呮椂

銆銆DriverManager.setLoginTimeout(10);

銆銆file://鎵撳紑涓涓繛鎺

銆銆Connection connection = DriverManager.getConnection(url,login,password);

銆銆file://寰楀埌鏁版嵁搴撻┍鍔ㄧ▼搴忕増鏈

銆銆銆DatabaseMetaData conMD = connection.getMetaData();

銆銆銆System.out.println("Driver Name:\t" + conMD.getDriverName());

銆銆銆System.out.println("Driver Version:\t" + conMD.getDriverVersion());

銆銆file://閫夋嫨鏁版嵁搴
銆銆connection.setCatalog( "MyDatabase");

銆銆file://鍒涘缓Statement


銆銆Statement st = connection.createStatement();

銆銆file://鎵ц鏌ヨ

銆銆ResultSet rs = st.executeQuery("SELECT * FROM mytable");

銆銆file://鍙栧緱缁撴灉锛岃緭鍑哄埌灞忓箷

銆銆while (rs.next()){

銆銆銆銆銆for(int j=1; j<=rs.getMetaData().getColumnCount(); j++){

銆銆銆銆銆System.out.print( rs.getObject(j)+"\t");

銆銆銆銆銆}

銆銆銆System.out.println();

銆銆}

銆銆file://鍏抽棴瀵硅薄

銆銆st.close();

銆銆銆銆connection.close();

銆銆} catch(Exception e) {

銆銆銆銆e.printStackTrace();

銆銆}

 

TAG: JDBC jdbc Servlet 搴旂敤 楂樼骇
鎵撳嵃 | 鏀惰棌姝ら〉 |  鎺ㄨ崘缁欏ソ鍙 | 涓炬姤
涓婁竴绡 涓嬩竴绡
 

璇勫垎锛0

鍙戣〃璇勮
鏌ョ湅鍏ㄩ儴鍥炲銆愬凡鏈0浣嶇綉鍙嬪彂琛ㄤ簡鐪嬫硶銆