當前位置: 首頁>>代碼示例>>Java>>正文


Java DbUtils.loadDriver方法代碼示例

本文整理匯總了Java中org.apache.commons.dbutils.DbUtils.loadDriver方法的典型用法代碼示例。如果您正苦於以下問題:Java DbUtils.loadDriver方法的具體用法?Java DbUtils.loadDriver怎麽用?Java DbUtils.loadDriver使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.commons.dbutils.DbUtils的用法示例。


在下文中一共展示了DbUtils.loadDriver方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Connection conn = null;

    final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
    final String driver = "org.h2.Driver";
    final String usr = "sa";
    final String pwd = "";

    try {
        DbUtils.loadDriver(driver);
        conn = DriverManager.getConnection(url, usr, pwd);
        QueryRunner query = new QueryRunner();
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) query
                .query(conn, "select * from user", new MapListHandler());
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = (Map<String, Object>) mapList.get(i);
            System.out.println("------> " + map.get("userId") + "\t"
                    + map.get("firstName") + "\t" + map.get("emailId"));
        }
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:26,代碼來源:DbUtilsMapListHandler.java

示例2: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
        final String driver = "org.h2.Driver";
        final String usr = "sa";
        final String pwd = "";

        QueryRunner run = new QueryRunner();

        DbUtils.loadDriver(driver);
        Connection conn = DriverManager.getConnection(url, usr, pwd);
        // -----------------------------------------------------------------------------------

        try {
            List<Map<String, Object>> maps = run.query(conn,
                    "SELECT * FROM employee", new MapListHandler());
            System.out.println(maps);
        } finally {
            DbUtils.close(conn);
        }

    }
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:23,代碼來源:MapListHandlerExample.java

示例3: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
        final String driver = "org.h2.Driver";
        final String usr = "sa";
        final String pwd = "";

        QueryRunner run = new QueryRunner();

        DbUtils.loadDriver(driver);
        Connection conn = DriverManager.getConnection(url, usr, pwd);
        // -----------------------------------------------------------------------------------
        ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(
                Employee.class);

        try {
            Employee emp = run.query(conn,
                    "SELECT * FROM employee WHERE employeename=?",
                    resultHandler, "Jose");
            System.out.println(emp.getEmployeeId());
        } finally {
            DbUtils.close(conn);
        }

    }
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:26,代碼來源:BeanHandlerExample.java

示例4: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
        final String driver = "org.h2.Driver";
        final String usr = "sa";
        final String pwd = "";

        QueryRunner run = new QueryRunner();

        DbUtils.loadDriver(driver);
        Connection conn = DriverManager.getConnection(url, usr, pwd);
        // -----------------------------------------------------------------------------------

        try {
            List<Object[]> query = run.query(conn, "SELECT * FROM employee",
                    new ArrayListHandler());
            for (Object[] objects : query) {
                System.out.println(Arrays.toString(objects));
            }
        } finally {
            DbUtils.close(conn);
        }

    }
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:25,代碼來源:ArrayListHandlerExample.java

示例5: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
        final String driver = "org.h2.Driver";
        final String usr = "sa";
        final String pwd = "";

        QueryRunner run = new QueryRunner();

        DbUtils.loadDriver(driver);

        Connection conn = DriverManager.getConnection(url, usr, pwd);
        // -----------------------------------------------------------------------------------
        ResultSetHandler<List<Employee>> resultListHandler = new BeanListHandler<Employee>(
                Employee.class);

        try {
            List<Employee> empList = run.query(conn, "SELECT * FROM employee",
                    resultListHandler);
            System.out.println(empList);
        } finally {
            DbUtils.close(conn);
        }

    }
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:26,代碼來源:BeanListHandlerExample.java

示例6: conn

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static Connection conn() {
  try {
    Connection _xblockexpression = null;
    {
      DbUtils.loadDriver("com.mysql.jdbc.Driver");
      _xblockexpression = DriverManager.getConnection("jdbc:mysql://localhost:3306/tung?useUnicode=true&characterEncoding=UTF-8", "root", 
        "");
    }
    return _xblockexpression;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
開發者ID:East196,項目名稱:maker,代碼行數:14,代碼來源:Addresses.java

示例7: test

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public void test() {
    boolean success = DbUtils.loadDriver(getStandardDriverClassName());
    if (success) {
        status = DatabasePlatform.STATUS_OK;
    } else {
        status = DatabasePlatform.STATUS_DRIVER_NOT_FOUND;
    }
}
 
開發者ID:ManyDesigns,項目名稱:Portofino,代碼行數:9,代碼來源:AbstractDatabasePlatform.java

示例8: initializeDataSource

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
/**
    * @param config
    * @throws SQLException
    */
private void initializeDataSource(Config config) {
       String user = config.getString(CONFIG_KEY_DB_USER);
       String password = config.getString(CONFIG_KEY_DB_PASSWORD);

       Properties params = new Properties();

       if (isNotEmpty(user) && isNotEmpty(password)) {
           params.put("user", user);
           params.put("password", password);
       }

       // ドライバのロード
       String driver = config.getString(CONFIG_KEY_DB_DRIVER);
       boolean loadSuccess = DbUtils.loadDriver(driver);
       if (!loadSuccess) {
           String message = "failed to load driver.";
           throw new RuntimeException(message);
       }

       // コネクションをプールするDataSource を作成する
       @SuppressWarnings("rawtypes")
    GenericObjectPool pool = new GenericObjectPool();
       // コネクションプールの設定を行う
       int  maxActive = config.getInt(CONFIG_KEY_DB_MAX_ACTIVE_CONN, 100);
       long maxWait   = Long.parseLong(config.getString(CONFIG_KEY_DB_WAIT, "-1"));
       pool.setMaxActive(maxActive);
       pool.setMaxIdle(maxActive);
       pool.setMaxWait(maxWait);

       driverUrl = config.getString(CONFIG_KEY_DB_URL);
       ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, params);
       new PoolableConnectionFactory(connFactory, pool, null,
               null, // validationQuery
               false, // defaultReadOnly
               false); // defaultAutoCommit
       dataSource = new PoolingDataSource(pool);
   }
 
開發者ID:okinawaopenlabs,項目名稱:of-patch-manager,代碼行數:42,代碼來源:ConnectionManagerJdbc.java

示例9: connect

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
/**
 * Connect to the database.
 *
 * @param driver Database driver class
 * @param url    URL for the connection.
 * @param user   Username. If null, the connection is made without using a username and password.
 * @param pwd    Password if needed.
 * @throws ClassNotFoundException  thrown if the JDBC driver cannot be found.
 * @throws SQLException     thrown if there is an exception with the query or result set.
 */
protected void connect(String driver, String url, String user, String pwd) throws ClassNotFoundException, SQLException {
    if(DbUtils.loadDriver(driver)) {

        if (!StringUtils.isBlank(user)) {
            this.mConn = DriverManager.getConnection(url, user, pwd);
        } else {
            this.mConn = DriverManager.getConnection(url);
        }
    } else {
        throw new ClassNotFoundException("Driver not found: " + driver);
    }
}
 
開發者ID:department-of-veterans-affairs,項目名稱:Leo,代碼行數:23,代碼來源:LeoDataSource.java

示例10: test

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public void test() {
    boolean success = DbUtils.loadDriver(getStandardDriverClassName());
    if (success) {
        status = STATUS_OK;
    } else {
        status = STATUS_DRIVER_NOT_FOUND;
    }
}
 
開發者ID:hongliangpan,項目名稱:manydesigns.cn,代碼行數:9,代碼來源:AbstractDatabasePlatform.java

示例11: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
    final String driver = "org.h2.Driver";
    final String usr = "sa";
    final String pwd = "";

    List<Map<String, Object>> userMaps = null;
    try {
        // Loading the Driver using DbUtils static method
        DbUtils.loadDriver(driver);
        conn = DriverManager.getConnection(url, usr, pwd);
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM user");
        MapListHandler mapListHandler = new MapListHandler();
        userMaps = mapListHandler.handle(rs);

        for (Map<String, Object> mapObj : userMaps) {
            System.out.println("User Object::  " + mapObj.get("userId")
                    + "\t" + mapObj.get("lastName") + "\t"
                    + mapObj.get("phoneNo"));
        }

    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        // Closing the connection quietly, means it will handles the
        // SQLException
        DbUtils.closeQuietly(conn);
    }
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:35,代碼來源:DbUtilsTest2.java

示例12: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Connection conn = null;

    final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
    final String driver = "org.h2.Driver";
    final String usr = "sa";
    final String pwd = "";

    List<User> users = null;

    try {
        DbUtils.loadDriver(driver);
        conn = DriverManager.getConnection(url, usr, pwd);
        QueryRunner query = new QueryRunner();
        users = (List<User>) query.query(conn, "select * from user",
                new BeanListHandler<User>(User.class));
        for (int i = 0; i < users.size(); i++) {
            User bean = users.get(i);
            System.out.println("User Objects::  " + bean.getUserId() + "\t"
                    + bean.getFirstName() + "\t" + bean.getLastName()
                    + "\t" + bean.getEmailId());
        }
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:29,代碼來源:DbUtilsBeanListHandler.java

示例13: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
    final String driver = "org.h2.Driver";
    final String usr = "sa";
    final String pwd = "";

    List<User> users = null;
    try {
        DbUtils.loadDriver(driver);
        conn = DriverManager.getConnection(url, usr, pwd);
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM user");
        BeanListHandler<User> listHandler = new BeanListHandler<User>(
                User.class);
        users = listHandler.handle(rs);

        for (User user : users) {
            System.out.println("User Object:: " + user.getUserId() + "\t"
                    + user.getFirstName() + "\t" + user.getEmailId());
        }

    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:32,代碼來源:DbUtilsTest1.java

示例14: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Connection conn = null;

    final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
    final String driver = "org.h2.Driver";
    final String usr = "sa";
    final String pwd = "";

    User user = null;

    try {
        // Loading the Driver using DbUtils static method
        DbUtils.loadDriver(driver);
        conn = DriverManager.getConnection(url, usr, pwd);
        QueryRunner query = new QueryRunner();
        user = (User) query.query(conn,
                "select * from user where userId=3", new BeanHandler<User>(
                        User.class));

        System.out.println("User Object::  " + user.getUserId() + "\t"
                + user.getFirstName() + "\t" + user.getLastName() + "\t"
                + user.getEmailId());

    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:30,代碼來源:DbUtilsBeanHandler.java

示例15: main

import org.apache.commons.dbutils.DbUtils; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
        final String driver = "org.h2.Driver";
        final String usr = "sa";
        final String pwd = "";

        QueryRunner run = new QueryRunner();

        DbUtils.loadDriver(driver);
        Connection conn = DriverManager.getConnection(url, usr, pwd);
        // -----------------------------------------------------------------------------------

        try {
            // Execute the SQL update statement and return the number of
            // inserts that were made
            int inserts = run.update(conn,
                    "INSERT INTO employee (employeename) VALUES (?)", "Arun");
            // The line before uses varargs and autoboxing to simplify the code
            System.out.println("inserts " + inserts);
            // Now it's time to rise to the occation...
            int updates = run.update(conn,
                    "UPDATE employee SET employeename=? WHERE employeeid=?",
                    "Simon", 1);
            System.out.println("updates " + updates);
        } catch (SQLException sqle) {
            // Handle it
            sqle.printStackTrace();
        }

    }
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:32,代碼來源:DBUtilsInsertUpdateExample.java


注:本文中的org.apache.commons.dbutils.DbUtils.loadDriver方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。