zoukankan      html  css  js  c++  java
  • JDBC连接访问SQL Server2012

     1 package com.pkg1;
     2 
     3 import java.sql.*;
     4 import com.microsoft.sqlserver.jdbc.*;
     5 
     6 public class JdbcDemo2 {
     7     Connection conn;
     8     PreparedStatement st;
     9     final static String connUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=TestDB";
    10     final static String dbUserName = "sa";
    11     final static String dbPwd = "123456";
    12     
    13     public JdbcDemo2(){
    14         try{
    15             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    16             this.conn = DriverManager.getConnection(connUrl, dbUserName, dbPwd);
    17         }catch(Exception e){
    18             e.printStackTrace();
    19             this.close();
    20         }
    21     }
    22     
    23     public ResultSet runQuery(String sql){
    24         ResultSet rs = null;
    25         try{
    26             this.st = this.conn.prepareStatement(sql);
    27             rs = this.st.executeQuery();
    28         }catch(Exception e){
    29             this.close();
    30             return null;
    31         }
    32         
    33         return rs;
    34         
    35     }
    36     
    37     public void close(){
    38         try{
    39             if(this.st != null){
    40                 this.st.close();
    41             }
    42             
    43             if(this.conn != null){
    44                 this.conn.close();
    45             }
    46         }catch(Exception e){
    47             e.printStackTrace();
    48         }
    49     }
    50     
    51 }
  • 相关阅读:
    mysql第三天作业
    mysql第二天作业
    mysql第一天作业
    S5第一次月考
    网络编程(待补充)
    字符编码(待补充)
    继承和封装
    面向对象
    codeforces 394E Lightbulb for Minister 简单几何
    跟面试官讲Binder(零)
  • 原文地址:https://www.cnblogs.com/guanhao/p/4926150.html
Copyright © 2011-2022 走看看