Java Code Examples for org.apache.commons.dbutils.DbUtils#loadDriver()

The following examples show how to use org.apache.commons.dbutils.DbUtils#loadDriver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbUtilsMapListHandler.java    From maven-framework-project with MIT License 8 votes vote down vote up
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);
	}
}
 
Example 2
Source File: MapListHandlerExample.java    From maven-framework-project with MIT License 6 votes vote down vote up
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);
		}

	}
 
Example 3
Source File: BeanHandlerExample.java    From maven-framework-project with MIT License 6 votes vote down vote up
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);
		}

	}
 
Example 4
Source File: ArrayListHandlerExample.java    From maven-framework-project with MIT License 6 votes vote down vote up
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);
		}

	}
 
Example 5
Source File: BeanListHandlerExample.java    From maven-framework-project with MIT License 6 votes vote down vote up
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);
		}

	}
 
Example 6
Source File: DbUtilsTest2.java    From maven-framework-project with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 7
Source File: DbUtilsBeanListHandler.java    From maven-framework-project with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 8
Source File: DbUtilsTest1.java    From maven-framework-project with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 9
Source File: DbUtilsBeanHandler.java    From maven-framework-project with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 10
Source File: DBUtilsInsertUpdateExample.java    From maven-framework-project with MIT License 5 votes vote down vote up
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();
		}

	}