Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);//依据不同数据库,加载不同驱动 String url = “jdbc:sqlserver://127.0.0.1:1433;databaseName=dbname” ; //数据库的连接地址(dbname为库名) String username = “sa”; //用户名 String password = "123456"; //密码 try{ //连接数据库 Connection con = DriverManager.getConnection(url , username , password ) ; Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(sql); } catch (ClassNotFoundException e) { e.printStackTrace(); }