JDBC

2023-04-24 13:52 更新

sqlserver简单连接调用

    public class Mftest {
        public static List<Map<Object,Object>> getData(){
            ResultSet rs=null;
            Connection cn=null;
            PreparedStatement stmt = null;
            List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
            try {
                cn=getCon();
                String sql="select date week_of_work,year,month,business_center, screen_type,sum(plan_qty) as plan_qty,sum(out_qty) as out_qty,sum(out_ok_qty) as out_ok_qty from IF_DailyReport" +
                        " where date between '2017-01-20' and '2017-01-31' and screen_type='曲面' group by date,week_of_work,year,month,business_center, screen_type";
                stmt=cn.prepareStatement(sql); 
                rs=stmt.executeQuery();
                ResultSetMetaData rsmd = rs.getMetaData();
                while(rs.next()){
                     Map<Object,Object> m = new HashMap<Object,Object>(rsmd.getColumnCount());
                        for(int i=1;i<=rsmd.getColumnCount();i++){
                            m.put(rsmd.getColumnName(i), rs.getObject(i));
                        }
                        list.add(m);
                }
                System.out.println(list);
                System.out.println("listsize="+list.size());

                
            }catch(Exception e){
                e.printStackTrace();
            }
            return list;
    }
            public static Connection getCon(){
                  String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                  String dbURL = "jdbc:sqlserver://10.126.72.226:1433;DatabaseName=MESDW";
                  String userName = "mobileuser";
                  String userPwd = "11111"; 
                  Connection dbConn = null;
                  try {
                   Class.forName(driverName);
                   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
                   System.out.println("Connection Successful!");
                  } catch (Exception e) {
                   e.printStackTrace();
                  }
                return dbConn;
            }
            public static void main(String[] args){
                getData();
            }
    }

    
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号