IDEA连接SQL Server

package ServletDemo;

import java.sql.*;

public class Datebase {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        //数据库URL[即:是要连接那一个数据库]localhost--指定本机;integratedSecurity=true--指定windows验证模式连接
        String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";//windows集成模式连接
//        String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login";
//        String username = "hh";
//        String passWord = "123";
//        Connection comm = DriverManager.getConnection(url,username,passWord);
        //建立连接
        Connection comm = DriverManager.getConnection(url);
        String SQL = "SELECT * FROM [user]";//注意:表名一定要带[]
        Statement stmt = comm.createStatement();
        //结果集
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next()){
            System.out.println(rs.getString(1)+""+rs.getString(2));
        }
        rs.close();
        comm.close();
    }
}

运行结果: 

原文地址:https://www.cnblogs.com/zhahu/p/11931385.html