隨筆 - 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

          主站蜘蛛池模板: 石城县| 普兰店市| 内乡县| 涿州市| 玉龙| 军事| 洪江市| 玛曲县| 阿瓦提县| 怀柔区| 迁西县| 林甸县| 通山县| 连平县| 白水县| 洱源县| 博罗县| 镇远县| 铜梁县| 许昌市| 大石桥市| 合肥市| 阳高县| 神木县| 闽清县| 同心县| 保康县| 江源县| 开远市| 肃宁县| 郯城县| 榆林市| 黑河市| 崇左市| 黄陵县| 个旧市| 阳城县| 灵石县| 北安市| 建德市| 安阳市|