JDBC 使用這個是MySQL下的

來源:互聯網
上載者:User

標籤:http   io   os   使用   ar   java   for   資料   sp   

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MysqlJDBC {
 
 @SuppressWarnings("rawtypes")
 public static List<Map> findsql(String sql){
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
         try {
           //第一步:載入MySQL的JDBC的驅動
              Class.forName("com.mysql.jdbc.Driver");
            //取得串連的 url,能訪問MySQL資料庫的使用者名稱,密碼;資料庫名
           String url = "jdbc:mysql://192.168.1.10:3306/shaimei";
           String user = "cpdmembers";
           String password = "cpdmembers";
           //第二步:建立與MySQL資料庫的串連類的執行個體
           conn = DriverManager.getConnection(url, user, password);
           //第三步:用conn建立Statement對象類執行個體 stmt
           stmt = conn.createStatement();
           //第四步:執行查詢,用ResultSet類的對象,返回查詢的結果
           //String sql = "select * from rerule";
           rs = stmt.executeQuery(sql);
           List<Map> list = new ArrayList<Map>();
           while(rs.next()){
               Map<String, Object> map = new HashMap<String, Object>(); 
            ResultSetMetaData rsmd = rs.getMetaData(); 
            int count = rsmd.getColumnCount();  
            for(int i=1;i<count;i++){
             String key = rsmd.getColumnLabel(i); 
             Object value = rs.getObject(i) ;
                map.put(key, value);
             
                list.add(map);
            }
            System.out.println(list); //取得資料庫中的資料
            return list;
               
           }
         } catch (ClassNotFoundException e) { 
         //載入JDBC錯誤,所要用的驅動沒有找到
          System.out.println("驅動載入錯誤");
   }catch (SQLException ex) {
     //顯示資料庫連接錯誤或查詢錯誤
     System.err.println("SQLException:"+ex.getMessage());
   }finally {
         try{
          if(rs != null) {
           rs.close();
           rs = null;
          }
          if(stmt != null) {
           stmt.close();
           stmt = null;
          }
          if(conn != null) {
           conn.close();
           conn = null;
          }
         }catch(SQLException e) {
          System.err.println("SQLException:"+e.getMessage());
         }
    }
  return null;
  }
 public static void main(String[] args) {
  //MySQL訪問包:http://download.csdn.net/detail/u010696272/8006839
  System.out.println(findsql("select * from rerule"));
 }
}

JDBC 使用這個是MySQL下的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.