---
title: "jdbc连接"
author: "xvm03"
date: "2010-08-02T17:43:56Z"
tags: []
categories: ["编程语言"]
slug: "jdbc"
draft: false
lastmod: "2010-08-02T18:47:47Z"
id: 727335
url: "https://xvm03.iteye.com/blog/727335"
---

Connection conn = null;  
ResultSet rs = null;  
preparedStatement pss = null;

1. //CallableStatement cstmt = null;

try  
{  
 Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );  
        conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=master;Uid=sa;Pwd=1234;","sa","1234");       
        pss = conn.prepareStatement("SELECT SAVESERIALZEDDATA FROM SESSION.pINGSESSION3DATA WHERE SESSIONKEY = ?");  
        pss.setString(1,sessionKey);  
        rs = pss.executeQuery();

 

1. //cstmt =conn.prepareCall("{CALL demoSp(? , ?)}") ;

 

 while(rs.next()){  
   String name=rs.getString("name");  
   String pass = rs.getString(1) ; // 此方法比较高效  
 }  
 rs.close();  
        pss.close();  
        conn.close();  
}  
catch (Exception t)  
{  
        // Insert Appropriate Error Handling Here  
}  
finally  
{  
        // The finally clause is always executed - even in error  
        // conditions preparedStatements and Connections will always be closed  
   
 try  
        {  
                  if (rs != null)  
                        rs.close();  
        }

 

 

 

 

        try  
        {  
                  if (pss != null)  
                         pss.close();  
        }  
        catch(Exception e) {}  
        try  
        {  
                  if (conn != null)  
                         conn.close();  
        }  
        catch (Exception e){}  
        }  
}