Java 连接Sql server

下载

下载驱动

连接

        String   driverName   =   "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        //这一句导致了最多的错误,等等再解释
        String   dbURL   =   "jdbc:sqlserver://localhost:1433;databaseName=SYB_IMS_API"; //;integratedSecurity=true
        String userName = "sa";   //默认用户名 
        String userPwd = "sa";   //密码
        try {
            Class.forName(driverName);
            conn_sqlserver = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
            stmt_sqlserver=conn_sqlserver.createStatement();
        } catch (Exception e) {
             e.printStackTrace();
             return false;
        }
        return true;

查询

        String sql="SELECT * FROM CRM_CATEGORY";
        try {
            msCategory= stmt_sqlserver.executeQuery( sql );
        } catch (SQLException e) {
            msCategory=null;
            e.printStackTrace();
        }

错误处理

2015-9-28 9:25:30 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
警告: Failed to load the sqljdbc_auth.dll

你可能感兴趣的:(Java 连接Sql server)