jdbc数据库链接(sql server)

来源:互联网 发布:郑州关键词优化so82 编辑:程序博客网 时间:2024/04/29 20:44
private static String dbName = "";    private static String user = "";    private static String password = "";    private static  String url = "jdbc:sqlserver://localhost:1433;databaseName="+dbName+";user="+user+";password="+password;      private static DataSource dataSource = null;    static{        try{            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");        } catch (Exception e){            System.out.println("Exception:"+e.getMessage()+"");              throw new ExceptionInInitializerError(e);         }    }    public static Connection getConnection() throws SQLException{        Connection conn = DriverManager.getConnection(url);//      conn.setTransactionIsolation(4096);        return conn;      }      public static DataSource getDataSource(){          return dataSource;      }     public static PreparedStatement prepareStatement(Connection con, String sql) {        PreparedStatement stmResult = null;        try {            stmResult = con.prepareStatement(sql);        } catch (Exception e) {        }        return stmResult;    }    public static void free(ResultSet rs,Statement st,Connection conn){          try{              if(rs != null){                  rs.close();              }          }catch(SQLException e){              e.printStackTrace();          }finally{              try{                  if(st != null){                      st.close();                  }              }catch(SQLException e){                  e.printStackTrace();              }finally{                  try{                      if(conn != null){                          conn.close();                      }                  }catch(SQLException e){                      e.printStackTrace();                  }              }          }      }    final static public String SELECT = "SELECT * FROM tablename";    public static void main(String[] args) {        try {            long begin = System.currentTimeMillis();            Connection connection  = getConnection();            PreparedStatement psPreparedStatement = connection.prepareStatement(SELECT);            ResultSet resultSet =psPreparedStatement.executeQuery();            int cc = 0;            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();            if(null != resultSet){                ResultSetMetaData mtdResults = resultSet.getMetaData();                cc =  mtdResults.getColumnCount();                while(resultSet.next()){                    Map<String, Object> map = new HashMap<String, Object>();                    for(int i= 1;i<=cc;i++){                        String colnameString = mtdResults.getColumnLabel(i);                        int type = mtdResults.getColumnType(i);                        //这里需要根据type来get相应的值,方法在下面                        Object colValue = resultSet.get...(i);                        map.put(colnameString, colValue);                    }                    list.add(map);                }             }            free(resultSet, psPreparedStatement, connection);            long end = System.currentTimeMillis();            long diff = end - begin;            System.out.println(diff);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }
这个是转自他http://my.csdn.net/MyXin的回答switch(type){    case Types.LONGVARCHAR: //-1        dataType="Long";       break;    case Types.CHAR:    //1       dataType="Character";       break;    case Types.NUMERIC: //2       switch(scale){        case 0:          dataType="Number";           break;        case -127:          dataType="Float";           break;        default:          dataType="Number";       }       break;    case Types.VARCHAR:  //12       dataType="String";       break;    case Types.DATE:  //91       dataType="Date";       break;    case Types.TIMESTAMP: //93       dataType="Date";       break;    case Types.BLOB :       dataType="Blob";       break;    default:       dataType="String";}
原创粉丝点击