| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:通讯录和数据库(MYSQL)相连
只看楼主 加入收藏
麒麟崽子
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
结帖率:100%
收藏
 问题点数:0 回复次数:0 
通讯录和数据库(MYSQL)相连
package com.softeem.jps;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBConnection {
    /** driver name */
    private static String driverName = "";

    /** url name */
    private static String url = "";

    /** user name */
    private static String userName = "";

    /** user password */
    private static String userPass = "";

    /**
     * 不能创建实例
     */
    private DBConnection() {

    }


    /**
     * 跟据用户名,密码得到数据库连接
     *
     * @param _userName
     * @param _userPass
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection(String _userName, String _userPass)
            throws ClassNotFoundException, SQLException {
        // mysql
        driverName = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://127.0.0.1:3306/db_user?characterEncoding=gbk";

        // sql server 2005
        // driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        // url = "jdbc:sqlserver://127.0.0.1:1433;database=mytest";

        // oracle
        // driverName = "oracle.jdbc.driver.OracleDriver";
        // url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

        userName = _userName;
        userPass = _userPass;

        Connection conn = null;
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, userName, userPass);

        return conn;
    }
    
    /**
     * 得到数据库连接
     *
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException,
            SQLException {
        return getConnection("root","19880629");
    }

    /**
     * 关闭数据库连接
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List executeQuery(String sql) {
        List list = new ArrayList();
        // jdbc...
        return list;
    }

    public static int executeUpdate(String sql) {
        int result = -1;
        // jdbc...
        return result;
    }

    /**
     * 测试及使用示例
     *
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = null;
        try {
            conn = getConnection("root", "19880629");
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        closeConnection(conn);
    }

}
搜索更多相关主题的帖子: 数据库连接 
2009-08-16 18:27
快速回复:通讯录和数据库(MYSQL)相连
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034508 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved