Servlet涓璲dbc搴旂敤楂樼骇绡囷紙涓锛
鍙戝竷: 2008-12-03 23:08 | 浣滆: 缃戠粶杞浇 | 鏉ユ簮: 缃戠粶杞浇 | 鏌ョ湅: 18娆
Jdbc:
銆銆鍏朵腑瀛愬崗璁紙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(鈥
銆銆鐒跺悗锛孌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();
銆銆}




发表于 2009-1-5 19:48
| 











