jdbc直接去访问数据得到数据

来源:互联网 发布:ubuntu 22 端口不通 编辑:程序博客网 时间:2024/04/27 21:31

 通过jdbc直接去访问数据得到数据

public List get_059sjs_date(final String kssj) {
  
  String stringsql = null;
  stringsql = "select * from 059sjs where DATE_FORMAT(kssj, '%Y-%m-%d')=?";

  List list = new ArrayList();
  
  PreparedStatement pstmt = null;
  final Connection cn = getConnection(); //getConnection方法在下面   
               try {
      pstmt = cn.prepareStatement(stringsql);
       pstmt.setString(1, kssj);  
                final ResultSet rs = pstmt.executeQuery();  
                while (rs.next())  
                {  
                 _059SJS ajj059 = new _059SJS();
                 ajj059.setKbh(rs.getLong("kbh"));
                 ajj059.setKscj(rs.getDouble("kscj"));
                 ajj059.setKssj(rs.getDate("kssj"));
                 ajj059.setKswyh(rs.getLong("kswyh"));
                 ajj059.setJj(rs.getString("jj"));
                 list.add(ajj059);
                }  
                rs.close();  
                pstmt.close(); 
                return list;
     } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     return list;    
 }

   
    /*
     * JDBC连接
     * */
    public static Connection getConnection()  
      {  
     
     
      Properties props = getProperties();///getProperties方法在下面
      String mysql_ip = props.getProperty("MYSQL_IP");
      String mysql_username=props.getProperty("MYSQL_USERNAME");
      String mysql_password=props.getProperty("MYSQL_PASSWORD");
//            final String url = "jdbc:mysql://localhost:3306/exam_hz";  
//            final String sUsr = "root";  
//            final String sPwd = "root";  
            try 
            {  
                Class.forName("org.gjt.mm.mysql.Driver");  
                return DriverManager.getConnection(mysql_ip, mysql_username, mysql_password);  
            }  
            catch (final ClassNotFoundException e)  
            {  
                //TODO 找不到驱动  
            }  
            catch (final SQLException e)  
            {  
                //TODO 创建连接异常  
            }  
            return null;  
        } 
    /*
  * 从service.txt文件中加载以等号分割的信息为键值对
  * @return Properties封装的键值对
  */
 private static Properties getProperties() {
  Properties props = new Properties();
  try {
   props.load(ajj_hz_serverClient.class.getResourceAsStream("../service.txt"));
  } catch (IOException e) {
   e.printStackTrace();
  }
  return props;
 }