java连接sqlserver数据库

 

 

package com.ziyun.cms.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SqlServerUtil {
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String URL = "jdbc:sqlserver://localhost:1433;databasename=";
    private static final String DATABASE_NAME = "test";
    private static final String USER_NAME = "sa";
    private static final String PASSWORD = "123456";
    
    public static void main(String[] args) {
        query();
    }
    
    public static void query(){
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try{
            Class.forName(DRIVER);           
            conn = DriverManager.getConnection(URL+DATABASE_NAME,USER_NAME,PASSWORD);
            //新建一个查询
            stmt = conn.createStatement();
            //执行查询-->>返回一个结果集
            rs = stmt.executeQuery("select * from dbo.t_user");    //括号里可以写相关的SQL语句,并把查询到的所有,放到一个rs集合里
            while (rs.next() ) {
                 log.info(rs.getString(1).trim() + "   " + rs.getString(2).trim()+ "   " + rs.getString(3).trim());
            }
        }catch(ClassNotFoundException e){
            log.error("驱动问题{}", e.getMessage());
        }catch(SQLException e){
            log.error("发生异常:{}", e.getMessage());
        }finally {
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static boolean testConnect(){
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try{
            Class.forName(DRIVER);           
            conn = DriverManager.getConnection(URL+DATABASE_NAME,USER_NAME,PASSWORD);
            return true;
        }catch(ClassNotFoundException e){
            log.error("驱动问题{}", e.getMessage());
        }catch(SQLException e){
            log.error("发生异常:{}", e.getMessage());
        }finally {
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
    
}

 

posted @ 2021-05-17 19:57  这个名字想了很久~  阅读(269)  评论(0编辑  收藏  举报