QueryJDBC.java~2~
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:1k
源码类别:
Java编程
开发平台:
Java
- package com.cwj.DatabaseAccessDemo;
- import java.sql.*;
- public class QueryJDBC {
- public QueryJDBC(){
- ResultSet result=null;
- Connection conn=null;
- Statement stat=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));
- }
- if(result!=null){
- result.close();
- }
- if(stat!=null){
- result.close();
- }
- if(conn!=null){
- conn.close();
- }
- }catch(Exception e){
- System.out.println("Error"+e);
- }
- }
- }