废话不多话,直接上代码了;这个demo我也是网上找的 稍微的修改了下;代码里面都有注释;
package com.example.democrud.democurd; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class sqlserver { public static void main(String[] args) { test1(); // test2(); } public static void test1(){ //默认sql server 驱动类 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //此处localhost 为本地的地址 1433 默认端口 demo 为主表 的名字 String dbURL = "jdbc:sqlserver://localhost:1433;databaseName=demo"; String userName = "sa"; //sqlserver用户名 String userPwd = "root"; //sqlserver用户密码 try { Class.forName(driverName); //加载sqlserver的驱动类 System.out.println("sqlServer load success!"); } catch (ClassNotFoundException a) { System.out.println("sqlServer load failed!"); a.printStackTrace(); } Connection dbcon = null; try { //处理与数据库的连接 dbcon = DriverManager.getConnection(dbURL, userName, userPwd); Statement stmt = dbcon.createStatement(); String sql = "select * from Demo"; //ResultSet对象,其包含给定查询生成的数据 ResultSet resultSet = stmt.executeQuery(sql); //获取执行的SQL语句的元数据 ResultSetMetaData metaData = resultSet.getMetaData(); //获取执行的SQL语句查询的列的个数 int columnCount = metaData.getColumnCount(); //获取表中的所有数据,字段 + 值 List<Map<String, Object>> list1 = new ArrayList<>(); List<Map<String, Object>> list2 = new ArrayList<>(); List<Object> list3 = new ArrayList<>(); //取出ResultSet中表的记录。如果可以取出则返回true,否则返回false while (resultSet.next()) { //数据 Map<String, Object> row = new HashMap<>(); //表结构--包含字段类型 Map<String, Object> map = new HashMap<>(); for (int i = 0; i < columnCount; i++) { //字段 String columnLabel = metaData.getColumnLabel(i + 1); //字段类型 String columnTypeName = metaData.getColumnTypeName(i + 1); //值 Object value = resultSet.getObject(i + 1); row.put(columnLabel, value); map.put(columnLabel, columnTypeName); list3.add(value); } list1.add(row); list2.add(map); System.out.println("获取到的key的值"+list2); System.out.println("获取到的value的值"+list1); System.out.println("单独获取V的值"+list3); } System.out.println("sqlServer connect success! sql server 连接成功"); dbcon.close(); } catch (SQLException e) { System.out.println("sqlServer connect failed!"); e.printStackTrace(); } } public static void test2(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://120.27.229.213:65023;databaseName=davinci3"; String userName = "sa"; //sqlserver用户名 String userPwd = "BSa!67365021"; //sqlserver用户密码 try { Class.forName(driverName); //加载sqlserver的驱动类 System.out.println("sqlServer load success!"); } catch (ClassNotFoundException a) { System.out.println("sqlServer load failed!"); a.printStackTrace(); } Connection dbcon = null; try { //处理与数据库的连接 dbcon = DriverManager.getConnection(dbURL, userName, userPwd); Statement stmt = dbcon.createStatement(); String sql = "select * from display"; //ResultSet对象,其包含给定查询生成的数据 ResultSet resultSet = stmt.executeQuery(sql); //获取执行的SQL语句的元数据 ResultSetMetaData metaData = resultSet.getMetaData(); //获取执行的SQL语句查询的列的个数 int columnCount = metaData.getColumnCount(); //获取表中的所有数据,字段 + 值 List<Map<String, Object>> list1 = new ArrayList<>(); List<Map<String, Object>> list2 = new ArrayList<>(); //取出ResultSet中表的记录。如果可以取出则返回true,否则返回false while (resultSet.next()) { //数据 Map<String, Object> row = new HashMap<>(); //表结构--包含字段类型 Map<String, Object> map = new HashMap<>(); for (int i = 0; i < columnCount; i++) { //字段 String columnLabel = metaData.getColumnLabel(i + 1); //字段类型 String columnTypeName = metaData.getColumnTypeName(i + 1); //值 Object value = resultSet.getObject(i + 1); row.put(columnLabel, value); map.put(columnLabel, columnTypeName); } list1.add(row); list2.add(map); System.out.println(list1); System.out.println(list2); } System.out.println("sqlServer connect success!"); dbcon.close(); } catch (SQLException e) { System.out.println("sqlServer connect failed!"); e.printStackTrace(); } } }
有疑问可留言;大家一起交流;