QueryJDBC.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:1k
源码类别:
Java编程
开发平台:
Java
- package com.cwj.DatabaseAccessDemo; import java.sql.*; public class QueryJDBC { public QueryJDBC(){ Connection conn=null; Statement stat=null; ResultSet result=null; try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=pubs;User=sa;Password=sa"); stat=conn.createStatement(); result=stat.executeQuery("select * from Publishers"); System.out.println("编号"+" "+"出版社名称"+" "+"城市"); while(result.next()){ System.out.println(result.getString(1)+" "+result.getString(2)+" "+result.getString(3)); } }catch(Exception e){ System.out.println("Error"+e); } finally{ try {
- if (result != null) {
- result.close();
- }
- if (stat != null) {
- stat.close();
- }
- if (conn != null) {
- conn.close();
- }
- }
- catch (SQLException ex) {
- }
- } } }