<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    JDBC連接常用數據庫 基本的CURD

     JDBC連接各個數據庫的className與url

    JDBC連接DB2
     private String className="com.ibm.db2.jdbc.net.DB2Driver";
       private String url="jdbc:db2://localhost:8080/lwc";
    JDBC連接Microsoft SQLServer(microsoft)
     private String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";
     private String url="jdbc:microsoft:sqlserver://
      localhost:1433;SelectMethod=Cursor;dataBaseName=lwc";
    JDBC連接Sybase(jconn2.jar)
     private String className="com.sybase.jdbc2.jdbc.SybDriver";
       private String url="jdbc:sybase:Tds:localhost:2638";
    JDBC連接MySQL(mm.mysql-3.0.2-bin.jar)
       private String className="org.gjt.mm.mysql.Driver";
       private String url="jdbc:mysql://localhost:3306/lwc";
    JDBC連接PostgreSQL(pgjdbc2.jar)
       private String className="org.postgresql.Driver";
       private String url="jdbc:postgresql://localhost/lwc";
    JDBC連接Oracle(classes12.jar)
       private String className="oracle.jdbc.driver.OracleDriver";
       private String url="jdbc:oracle:thin:@localhost:1521:lwc";

      JDBC連接數據庫案例

    package com.itlwc;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class DBConnection {
     private static Connection conn = null;
     private String user = "";
     private String password = "";
     private String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
     private String url = "jdbc:microsoft:sqlserver://"
       + "localhost:1433;SelectMethod=Cursor;dataBaseName=lwc";

     private DBConnection() {
      try {
       Class.forName(this.className);
       conn = DriverManager.getConnection(url, user, password);
       System.out.println("連接數據庫成功");
      } catch (ClassNotFoundException e) {
       System.out.println("連接數據庫失敗");
      } catch (SQLException e) {
       System.out.println("連接數據庫失敗");
      }
     }

     public static Connection getConn() {
      if (conn == null) {
       conn = (Connection) new DBConnection();
      }
      return conn;
     }

     // 關閉數據庫
     public static void close(ResultSet rs, Statement state, Connection conn) {
      if (rs != null) {
       try {
        rs.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
       rs = null;
      }
      if (state != null) {
       try {
        state.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
       state = null;
      }
      if (conn != null) {
       try {
        conn.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
       conn = null;
      }
     }

     // 測試數據庫連接是否成功
     public static void main(String[] args) {
      getConn();
     }
    }

     基本CURD

    獲取數據庫連接請查考JDBC連接常用數據庫
    private Connection conn = DBConnection.getConn();

      增加方法

    使用拼sql增加
    public void add1(Student student) {
     String sql = "insert into student values(" + student.getId() + ",'"
       + student.getCode() + "','" + student.getName() + "',"
       + student.getSex() + "," + student.getAge() + ")";
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }
    使用替換變量增加
    public void add2(Student student) {
     String sql = "insert into student values(?,?,?,?,?)";
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.setString(1, student.getCode());
      ps.setString(2, student.getName());
      ps.setString(3, student.getSex());
      ps.setString(4, student.getAge());
      ps.setString(5, student.getId());
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }

      刪除方法

    使用拼sql刪除
    public void delete1(String id) {
     String sql = "delete from student where id='" + id+"'";
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }
    使用替換變量刪除
    public void delete2(String id) {
     String sql = "delete from student where id=?";
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.setString(1, id);
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }

     修改方法

    使用拼sql修改
    public void update1(Student student) {
     String sql = "update student set code='" + student.getCode()
       + "',name='" + student.getName() + "',sex=" + student.getSex()
       + ",age=" + student.getAge() + " where id=" + student.getId();
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }
    使用替換變量修改
    public void update2(Student student) {
     String sql = "update student set code=?,name=?,sex=?,age=? where id=?";
     PreparedStatement ps = null;
     try {
      ps = conn.prepareStatement(sql);
      ps.setString(1, student.getCode());
      ps.setString(2, student.getName());
      ps.setString(3, student.getSex());
      ps.setString(4, student.getAge());
      ps.setString(5, student.getId());
      ps.executeUpdate();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(null, ps, conn);
    }

      查詢方法

    查詢得到一個對象
    public Student findById(int id) {
     String sql = "select * from student where id=" + id;
     Student student = new Student();
     PreparedStatement ps = null;
     ResultSet rs = null;
     try {
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      if (rs.next()) {
       student.setId(rs.getString(1));
       student.setCode(rs.getString(2));
       student.setName(rs.getString(3));
       student.setSex(rs.getString(4));
       student.setAge(rs.getString(5));
      }
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(rs, ps, conn);
     return student;
    }
    查詢得到一組數據
    @SuppressWarnings("unchecked")
    public List find() {
     String sql = "select * from student";
     List list = new ArrayList();
     PreparedStatement ps = null;
     ResultSet rs = null;
     try {
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      while (rs.next()) {
       Student student = new Student();
       student.setId(rs.getString(1));
       student.setCode(rs.getString(2));
       student.setName(rs.getString(3));
       student.setSex(rs.getString(4));
       student.setAge(rs.getString(5));
       list.add(student);
      }
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(rs, ps, conn);
     return list;
    }


     統計數據庫總條數

    public int getRows() {
     int totalRows = 0;
     String sql = "select count(*) as totalRows from student";
     PreparedStatement ps = null;
     ResultSet rs = null;
     try {
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      if (rs.next()) {
       totalRows = Integer.valueOf(rs.getString("totalRows"));
      }
     } catch (SQLException e) {
      e.printStackTrace();
     }
     DBConnection.close(rs, ps, conn);
     return totalRows;
    }

      執行存儲過程

    第一種
     public String retrieveId(String tableName,String interval) throws SQLException {  
         Connection conn = DBConnection.getConn();  
         String sql = "exec p_xt_idbuilder '" + tableName + "','" + interval+ "'";  
         PreparedStatement ps = conn.prepareStatement(sql);
      ResultSet rs = ps.executeQuery();
         String maxId = "";  
         if(rs.next()){  
             maxId = rs.getString("bh");  
         }  
         DBConnection.close(rs, ps, conn);  
         return maxId;  
     }
    第二種
     public String retrieveId(String tableName,String interval) throws SQLException {
      Connection conn = DBConnection.getConn();
      CallableStatement cs = conn.prepareCall("{call p_xt_idbuilder(?,?,?)}");
         cs.setString(1, tableName);
         cs.setString(2, interval);
         cs.registerOutParameter(3,java.sql.Types.VARCHAR);
         cs.executeUpdate();
         String maxId = "";
         maxId=cs.getString(3);   
      DBConnection.close(null, cs, conn);
      return maxId;
     }


    posted on 2013-09-22 09:35 順其自然EVO 閱讀(292) 評論(0)  編輯  收藏 所屬分類: 數據庫

    <2013年9月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久久精品午夜免费不卡| 一个人看的hd免费视频| 最近免费中文字幕大全免费 | 成人A片产无码免费视频在线观看| 内射无码专区久久亚洲| 色一情一乱一伦一视频免费看| 日韩免费视频播放| 亚洲Av永久无码精品黑人| 国产免费牲交视频| 四虎成人精品国产永久免费无码| 高清在线亚洲精品国产二区| 一区二区三区免费电影| 国产亚洲精品成人a v小说| 国产免费网站看v片在线| 亚洲VA成无码人在线观看天堂| 国产成人精品无码免费看| 久久久久亚洲AV无码专区体验| 99爱免费观看视频在线| 亚洲美女激情视频| 毛色毛片免费观看| 午夜在线亚洲男人午在线| MM131亚洲国产美女久久| 嫩草在线视频www免费看| 67pao强力打造67194在线午夜亚洲 | 久久中文字幕免费视频| 亚洲国产精品乱码在线观看97| 在线看片免费不卡人成视频| 国产亚洲男人的天堂在线观看| 中文字幕第一页亚洲| 在线免费观看国产| 亚洲精品自偷自拍无码| 国产av无码专区亚洲国产精品| 七色永久性tv网站免费看| 亚洲国产视频久久| 久久久久一级精品亚洲国产成人综合AV区 | 伊人久久国产免费观看视频| 亚洲va在线va天堂va888www| 无码一区二区三区AV免费| yellow免费网站| 亚洲午夜精品在线| 日韩精品成人亚洲专区|