隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
          數據加載中……

          java連接MySql數據庫

          package com.roytel.rtccp.util;

           

          import java.sql.*;

           

          public class DBManager {

          //用戶名

            private String user = "";

          //密碼

            private String password = "";

          //主機

            private String host = "";

          //數據庫名字

            private String database = "";

           

            /*

               private String url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";

             */

            private String url ="";

            private Connection con = null;

           

            Statement stmt;

            /**

             * 根據主機、數據庫名稱、數據庫用戶名、數據庫用戶密碼取得連接。

             * @param host String

             * @param database String

             * @param user String

             * @param password String

             */

            public DBManager(String host, String database, String user, String password) {

           

              this.host = host;

              this.database = database;

              this.user = user;

          this.password = password;

          //顯示中文

              this.url = "jdbc:mysql://" + host + "/" + database +

                  "?useUnicode=true&characterEncoding=GB2312";

           

              try {

                Class.forName("org.gjt.mm.mysql.Driver");

              }

              catch (ClassNotFoundException e) {

                System.err.println("class not found:" + e.getMessage());

              }

           

              try {

                con = DriverManager.getConnection(this.url, this.user, this.password);

          //連接類型為ResultSet.TYPE_SCROLL_INSENSITIVE,                                 ResultSet.CONCUR_READ_ONLY

                stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                                           ResultSet.CONCUR_READ_ONLY);

              }

              catch (SQLException a) {

                System.err.println("sql exception:" + a.getMessage());

              }

            }

          /**

          * 返回取得的連接

          */

            public Connection getCon() {

              return con;

            }

          /**

          * 執行一條簡單的查詢語句

          * 返回取得的結果集

          */

            public ResultSet executeQuery(String sql) {

              ResultSet rs = null;

              try {

                rs = stmt.executeQuery(sql);

              }

              catch (SQLException e) {

                e.printStackTrace();

              }

              return rs;

            }

          /**

          * 執行一條簡單的更新語句

          * 執行成功則返回true

          */

            public boolean executeUpdate(String sql) {

              boolean v = false;

              try {

                v = stmt.executeUpdate(sql) > 0 ? true : false;

              }

              catch (SQLException e) {

                e.printStackTrace();

              }

              finally {

                return v;

              }

            }

           

          }

          能用的,你可以擴充,比如增加執行預編譯語句的方法、執行存儲過程的方法,也可以用連接池的方法。

          posted on 2005-02-12 23:24 閱讀(134) 評論(0)  編輯  收藏 所屬分類: J2ee

          主站蜘蛛池模板: 永善县| 乌兰察布市| 杂多县| 津市市| 沅江市| 克东县| 峨山| 嵊州市| 资中县| 衡水市| 锦屏县| 新竹市| 铁岭县| 甘南县| 富裕县| 托克托县| 大安市| 清苑县| 荃湾区| 博客| 电白县| 咸丰县| 堆龙德庆县| 新营市| 梧州市| 东安县| 邳州市| 肃北| 荥经县| 霍山县| 芦山县| 辛集市| 南川市| 河曲县| 绥宁县| 鄯善县| 郧西县| 富民县| 河北省| 东城区| 修水县|