- import java.sql.*;
- import java.util.LinkedList;
- import java.util.HashMap;
- import java.util.Iterator;
- import org.sqlite.JDBC;
- class DBConfig
- {
- private int m_Type; //数据库类型
- private int m_nPort; //数据库服务器端口
- private String m_sUrl; //数据库服务器URL/IP
- private String m_sName; //数据库名称
- private String m_sLoginUser; //登陆用户名
- private String m_sLoginPwd; //登陆密码
- public static final int CONN_MYSQL = 1;
- public static final int CONN_SQLLITE = 2;
- /*
- *构造函数
- **/
- public
- DBConfig( int type,
- int port,
- String url,
- String name,
- String user,
- String pwd
- )
- {
- m_Type = type;
- m_sUrl = url;
- m_nPort = port;
- m_sName = name;
- m_sLoginUser = user;
- m_sLoginPwd = pwd;
- }
- /*
- *配置数据库连接信息
- **/
- public void
- setInfo( int type,
- int port,
- String url,
- String name,
- String user,
- String pwd
- )
- {
- m_Type = type;
- m_sUrl = url;
- m_nPort = port;
- m_sName = name;
- m_sLoginUser = user;
- m_sLoginPwd = pwd;
- }
- /*
- *获取JDBC连接字符串
- *return
- * 返回连接字符串
- **/
- public String
- getConnStr()
- {
- if( m_sUrl == null || m_sName == null)
- {
- return null;
- }
- String szHead = null;
- String szConn = null;
- switch (m_Type)
- {
- case CONN_MYSQL:
- szHead = "jdbc:mysql://";
- szConn = szHead + m_sUrl + ":" + m_nPort + "/" + m_sName;//连接字符串
- break;
- case CONN_SQLLITE:
- szHead = "jdbc:sqlite:";
- szConn = szHead + m_sUrl + m_sName;//连接字符串
- break;
- default:
- return null;
- }
- return szConn;
- }
- /*
- *获取配置的数据库类型
- **/
- public int
- getType()
- {
- return m_Type;
- }
- /*
- *获取配置的数据库用户名
- **/
- public String
- getUserName()
- {
- return m_sLoginUser;
- }
- /*
- *获取配置的数据库密码
- */
- public String
- getPassword()
- {
- return m_sLoginPwd;
- }
- }
- public class DBEng
- {
- private DBConfig m_dbCfg;
- private boolean m_dbStatus;
- private Connection m_dbConn;//连接对象
- private Statement m_dbStatement;//访问接口
- public DBEng(DBConfig info)
- {
- m_dbCfg = info;
- m_dbStatus = false;
- m_dbConn = null;
- m_dbStatement = null;
- }
- public boolean
- init()
- {
- String szDriverClass = null;//驱动程序类名
- int type = m_dbCfg.getType();
- switch ( type )
- {
- case DBConfig.CONN_MYSQL :
- szDriverClass = "com.mysql.jdbc.Driver";
- break;
- case DBConfig.CONN_SQLLITE:
- szDriverClass = "org.sqlite.JDBC";
- break;
- default:
- return false;
- }
- try
- {
- Class.forName(szDriverClass);//反射获取
- return true;
- }
- catch(ClassNotFoundException e)
- {
- e.printStackTrace();//找不到驱动程序类 加载驱动失败
- return false;
- }
- }
- public boolean
- open()
- {
- if( m_dbStatus )return false;
- try
- {
- String szConn = m_dbCfg.getConnStr();
- if (szConn == null)
- {
- return false;
- }
- String szUser = m_dbCfg.getUserName();
- String szPwd = m_dbCfg.getPassword();
- //创建数据库连接对象
- m_dbConn = DriverManager.getConnection( szConn,
- szUser,
- szPwd
- );
- // 创建用于执行静态sql语句的Statement对象
- m_dbStatement = (Statement) m_dbConn.createStatement();
- m_dbStatus = true;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- m_dbStatus = false;
- }
- return m_dbStatus;
- }
- public boolean
- close()
- {
- if( !m_dbStatus )return false;
- try
- {
- m_dbStatement.close();
- m_dbConn.close();
- return true;
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public boolean
- exec(String sql)
- {
- if( !m_dbStatus )return false;
- try
- {
- m_dbStatement.executeUpdate(sql);
- m_dbStatement.close();
- return true;
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public boolean
- query(String sql,LinkedList<HashMap<String,String> > out_result)
- {
- if( !m_dbStatus || out_result == null)
- {
- return false;
- }
- else
- {
- out_result.clear();
- }
- try
- {
- ResultSet rs = m_dbStatement.executeQuery(sql);//查询结果记录集
- if(rs != null)
- {
- HashMap map = null; //保存一条记录的字段与内容的映射
- String key = null;
- String value = null;
- int nCount = 0; //记录条数总计
- ResultSetMetaData rsm = null;//列名和类型信息
- while ( rs.next() ) // 判断是否还有下一个数据
- {
- rsm = rs.getMetaData();
- nCount = rsm.getColumnCount();
- map = new HashMap();
- for(int i = 1; i <= nCount; i++)
- {
- key = rsm.getColumnName(i);
- value = rs.getString(key);
- map.put(key, value);
- }
- out_result.add(map);
- }
- }
- rs.close();
- m_dbStatement.close();
- return true;
- }
- catch(SQLException e)
- {
- return false;
- }
- }
- public void
- beginTrans()//开始事务
- {
- if(m_dbStatus)
- {
- try
- {
- m_dbConn.setAutoCommit(false);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public void
- rollbackTrans()//回滚操作
- {
- if(m_dbStatus)
- {
- try
- {
- m_dbConn.rollback();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public void
- commitTrans()//完成当前事务并保存变化
- {
- if(m_dbStatus)
- {
- try
- {
- m_dbConn.commit();
- m_dbConn.setAutoCommit(true);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public static void
- printRecodeset(LinkedList<HashMap<String,String> > recodeset)
- {
- if(recodeset == null)
- {
- return;
- }
- String key = null;
- String val = null;
- Iterator it_map = null;
- HashMap.Entry entry = null;
- for(int i=0; i < recodeset.size() ; ++i)
- {
- it_map = recodeset.get(i).entrySet().iterator();
- while ( it_map.hasNext() )
- {
- entry = (HashMap.Entry) it_map.next();
- key = (String)entry.getKey();
- val = (String)entry.getValue();
- System.out.println("字段:" + key + "---" + "内容" + val);
- }
- System.out.println("-------------------------------------");
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0211201513935.html
来源: http://www.codesnippet.cn/detail/0211201513935.html