隨筆 - 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 閱讀(137) 評論(0)  編輯  收藏 所屬分類: J2ee

          主站蜘蛛池模板: 商丘市| 惠来县| 韶关市| 宜兰县| 文水县| 寿宁县| 雷州市| 金川县| 新闻| 全南县| 垦利县| 禹城市| 高青县| 阳新县| 丰都县| 合山市| 泸州市| 沁阳市| 新昌县| 嵊州市| 台东市| 若尔盖县| 新蔡县| 调兵山市| 合水县| 辛集市| 汉寿县| 南投县| 永修县| 仁布县| 依兰县| 海原县| 阿拉善左旗| 保德县| 肇州县| 岱山县| 华安县| 肥西县| 绍兴县| 灵璧县| 卢湾区|