conndatabase.java~13~

来自「野生动物系统的JAVA源码, 野生动物系统的JAVA源码」· JAVA~13~ 代码 · 共 74 行

JAVA~13~
74
字号
package scout.database.util;

/**
 * <p>Title: </p>
 *
 * <p>Description:this class connect database of my sql,and return the object of connection </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Chongqing Kemeida corporation</p>
 *
 * @author Bozeng
 * @version 1.0
 */
import java.sql.*;
import java.util.*;

public class ConnDataBase {
    private String driverName = "";
    private String userName = "";
    private String dbPort = "";
    private String userPasswd = "";
    private String dbName = "";
    private String dbServer = "";

    /**
     * for init the parameter of connecting database
     */
    public ConnDataBase() {
        ReadXml rx = new ReadXml();
        Vector vv = new Vector();
        try {
            vv = rx.getVectorOfXml();
            this.driverName = vv.elementAt(0).toString();
            this.userName = vv.elementAt(1).toString();
            this.userPasswd = vv.elementAt(2).toString();
            this.dbName = vv.elementAt(3).toString();
            this.dbPort=vv.elementAt(4).toString();
            this.dbServer = vv.elementAt(5).toString();

        } catch (Exception e) {
            System.out.println(
                    "It's an error for init connection database parameter!");
        }
    }

    /**
     * the method is for return the connection of database
     * @return Connection
     */
    public Connection getConnection() {
        String url = "jdbc:mysql://" + dbServer +":"+dbPort+"/" + dbName + "?user=" +
                     userName + "&password=" + userPasswd;
        System.out.println(url);
        Connection connection = null;
        try {
            Class.forName(driverName).newInstance();
            connection = DriverManager.getConnection(url);
        } catch (Exception e) {
            System.out.println("connect database is error,try again!");
        }
        return connection;
    }

    /**
     * for testing
     * @param args String[]
     */
    public static void main(String[] args) {
        ConnDataBase cdb = new ConnDataBase();
        System.out.println(cdb.getConnection());
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?