Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> androidj常用數據庫操作JDBC Utils

androidj常用數據庫操作JDBC Utils

編輯:關於Android編程

package mypackage;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JDBCUtils {

	private final String USERNAME = "root";
	private final String PWD = "admin";
	private final String DRIVER = "com.mysql.jdbc.Driver";
	private Connection connection;
	private java.sql.PreparedStatement preparedStatement;
	private ResultSet resultset;
	private String URL = "jdbc:mysql://localhost:3306/mydb";

	public JDBCUtils() {
		try {
			Class.forName(DRIVER);
			System.out.println("注冊驅動成功");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	// 定義獲得數據庫的鏈接

	public Connection getConnection() {
		try {
			connection = DriverManager.getConnection(URL, USERNAME, PWD);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return connection;
	}

	/**
	 * 
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public boolean updateByPrepareStatement(String sql, List params)
			throws SQLException {
		boolean flag = false;
		int result = -1;// 受影響的行數
		int index = 1;
		preparedStatement = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				preparedStatement.setObject(index++, params.get(i));
			}
		}
		result = preparedStatement.executeUpdate();
		flag = result > 0 ? true : false;
		return flag;
	}

	/**
	 * 查詢返回單條記錄
	 * 
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public Map findSimpleResult(String sql, List params)
			throws SQLException {
		Map map = new HashMap();
		int index = 1;
		preparedStatement = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				preparedStatement.setObject(index++, params.get(i));
			}
		}
		resultset = preparedStatement.executeQuery();// 返回查詢結果
		ResultSetMetaData metaData = resultset.getMetaData();
		int col_len = metaData.getColumnCount();
		while (resultset.next()) {
			for (int i = 0; i < col_len; i++) {
				String col_name = metaData.getColumnName(i + 1);
				Object col_values = resultset.getObject(col_name);
				if (col_values == null) {
					col_values = "";
				}
				map.put(col_name, col_values);
			}
		}
		return map;
	}

	/**
	 * 查找多條記錄
	 * 
	 * @return
	 */

	public List> findMoreResultString(String sql,
			List params) throws SQLException {
		List> list = new ArrayList>();
		int index = 1;
		preparedStatement = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				preparedStatement.setObject(index++, params.get(i));
			}
		}
		resultset = preparedStatement.executeQuery();// 返回查詢結果
		ResultSetMetaData metaData = resultset.getMetaData();
		int col_len = metaData.getColumnCount();
		while (resultset.next()) {
			Map map = new HashMap();
			for (int i = 0; i < col_len; i++) {
				String col_name = metaData.getColumnName(i + 1);
				Object col_values = resultset.getObject(col_name);
				if (col_values == null) {
					col_values = "";
				}
				map.put(col_name, col_values);
			}

			list.add(map);
		}

		return list;
	}

	// 利用反射機制
	/**
	 * 通過反射機制訪問數據庫
	 * 
	 * @param sql
	 * @param params
	 * @param cls
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 * @throws IllegalAccessException
	 */
	public  List findMore(String sql, List params, Class cls)
			throws SQLException, Exception {
		List list = new ArrayList();
		int index = 1;
		Object resultObject=null;
		preparedStatement = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				preparedStatement.setObject(index++, params.get(i));
			}
		}
		resultset = preparedStatement.executeQuery();// 返回查詢結果
		ResultSetMetaData metaData = resultset.getMetaData();
		int col_len = metaData.getColumnCount();
		while (resultset.next()) {
			resultObject = cls.newInstance();
			for (int i = 0; i < col_len; i++) {
				String col_name = metaData.getColumnName(i+1);
				Object col_values = resultset.getObject(col_name);
				if (col_values == null) {
					col_values = "";
				}
				Field field = cls.getDeclaredField(col_name);
				field.setAccessible(true);
				field.set(resultObject, col_values);
			}
			list.add((T) resultObject);
		}

		return list;
	}

	/**
	 * 關閉數據庫連接
	 */
	public void releaseConnection() {

		if (resultset != null)
			try {
				resultset.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (preparedStatement != null)
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (connection != null)
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * 
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		JDBCUtils jdbcUtils = new JDBCUtils();
		jdbcUtils.getConnection();

		
		/*
		 * String sql = "insert into userinfo(username,pwd) values (?,?)";
		 * List params = new ArrayList(); params.add("admin");
		 * params.add("admin"); boolean flag =
		 * jdbcUtils.updateByPrepareStatement(sql, params);
		 * System.out.println(flag);
		 */
		String sql = "select * from userinfo where id=?";
		String sql1 = "select * from userinfo";
		List params = new ArrayList();
		/*params.add(1);
		Map map = jdbcUtils.findSimpleResult(sql, params);

		try {
			List list = jdbcUtils.findMoreResultString(sql1, null);
			System.out.println(map);
			System.out.println(list);
		}

		catch (Exception e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.releaseConnection();
		}*/
		try {
			System.out.println("---fsdfas");
		List userlist=	jdbcUtils.findMore(sql1, null, userinfo.class);
		System.out.println("---"+userlist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			jdbcUtils.releaseConnection();
		}
	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved