- package pubcls;
- import java.sql.*;
- /**
- * MySql 简单操作类
- * @author itblt
- */
- public class Mysql
- {
- public Connection liveCon = null;
- public int dbWaitTime = 3600;
- public String dbLang = "";
- public String errMessage = "";
- private String linkUrl = "";
- private String linkDb = "";
- private String linkUser = "";
- private String linkPwd = "";
- /**
- * 设置默认参数
- */
- private void setDefault() {
- this.linkUrl = "jdbc:mysql://localhost:3306/";
- this.linkDb = "test";
- this.linkUser = "root";
- this.linkPwd = "";
- this.dbLang = "utf8";
- }
- /**
- * 用默认参数初始化
- */
- public Mysql() {
- this.setDefault();
- }
- /**
- * 指定数据库及编码进行初始化
- * @parem String sdb
- * 数据库名
- * @parem String dblang
- * 编码(通常是utf8/gbk)
- *
- */
- public Mysql(String sdb, String dblang) {
- this.setDefault();
- this.linkDb = sdb;
- this.dbLang = dblang;
- }
- /**
- * 用完整的参数初始化
- * @parem String surl
- * 连接网址(jdbc:mysql://IP:端口/): jdbc:mysql://localhost:3306/
- * @parem String sdb
- * 数据库名
- * @parem String suser
- * 用户名
- * @parem String spwd
- * 密码
- * @parem String dblang
- * 数据库编码
- */
- public Mysql(String surl, String sdb, String suser, String spwd, String dblang) {
- this.linkUrl = surl;
- this.linkDb = sdb;
- this.linkUser = suser;
- this.linkPwd = spwd;
- this.dbLang = dblang;
- }
- /**
- * 获得链接
- */
- public Connection getConnection() throws SQLException,
- java.lang.ClassNotFoundException
- {
- this.liveCon = this.getConnection(this.linkUrl, this.linkDb, this.linkUser, this.linkPwd, this.dbLang);
- return this.liveCon;
- }
- /**
- * 用指定参数获取链接
- * @parem String surl
- * 连接网址(jdbc:mysql://IP:端口/): jdbc:mysql://localhost:3306/
- * @parem String sdb
- * 数据库名
- * @parem String suser
- * 用户名
- * @parem String spwd
- * 密码
- * @parem String dblang
- * 数据库编码
- * @return Connection
- */
- public Connection getConnection(String surl, String sdb, String suser, String spwd, String dblang) throws SQLException,
- java.lang.ClassNotFoundException
- {
- this.linkUrl = surl;
- this.linkDb = sdb;
- this.linkUser = suser;
- this.linkPwd = spwd;
- this.dbLang = dblang;
- Class.forName("org.gjt.mm.mysql.Driver");
- this.liveCon = DriverManager.getConnection(surl+sdb, suser, spwd);
- this.preQuery();
- return this.liveCon;
- }
- /**
- * mysql转义
- * @return String
- */
- public static String QuoteStr(String str)
- {
- String[] searchs = {"", "n", "r", "26", "t", "\", """, "'"};
- String[] replaces = {"\0", "\n", "\r", "\Z" , "\t", "\\", "\"", "\'"};
- for(int i=0; i < searchs.length; i++)
- {
- str = str.replace(searchs[i], replaces[i]);
- }
- return str;
- }
- /**
- * 测试链接
- * @return boolean
- */
- private boolean testCon() throws SQLException
- {
- if(this.liveCon==null || this.liveCon.isClosed() )
- {
- try
- {
- this.getConnection();
- return true;
- }catch(ClassNotFoundException e)
- {
- return false;
- }
- catch(SQLException e2)
- {
- this.errMessage = e2.getMessage();
- return false;
- }
- }
- return true;
- }
- /**
- * 执行写入语句
- * @parem string query
- * SQL 写入类操作
- * @return int
- */
- public int execUpdate(String query) throws SQLException
- {
- if(!this.testCon())
- {
- return -1;
- }
- Statement sql_statement = this.liveCon.createStatement();
- int result = sql_statement.executeUpdate(query);
- sql_statement.close();
- return result;
- }
- /**
- * 执行insert语句并返回上次插入的主链id
- * @parem string
- * sql SQL语句
- * @return int
- */
- public long execInsert(String query) throws SQLException
- {
- if(!this.testCon())
- {
- return -1;
- }
- Statement sql_statement = this.liveCon.createStatement();
- long result = sql_statement.executeUpdate(query);
- if(result>0)
- {
- sql_statement = this.getStatement(" Select LAST_INSERT_ID(); ");
- ResultSet rs = sql_statement.getResultSet();
- rs.next();
- result = rs.getLong(0);
- }
- sql_statement.close();
- return result;
- }
- /**
- * 获得一个记录集
- * @parem String query
- * 查询用的SQL语句
- * @return ResultSet
- */
- public ResultSet getResultSet(String query) throws SQLException
- {
- if(!this.testCon())
- {
- return null;
- }
- Statement sql_statement = this.getStatement(query);
- ResultSet result = sql_statement.getResultSet();
- return result;
- }
- /**
- * 获得一个可用于更新操作的记录集
- * @parem String query
- * 查询用的SQL语句
- * @return ResultSet
- */
- public ResultSet getResultSetUpdate(String query) throws SQLException
- {
- if(!this.testCon())
- {
- return null;
- }
- Statement sql_statement = this.liveCon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- sql_statement.executeQuery(query);
- ResultSet result = sql_statement.getResultSet();
- return result;
- }
- /**
- * 获得一个Statement
- * @parem String query
- * 查询sql
- * @return Statement
- */
- public Statement getStatement(String query) throws SQLException
- {
- if(!this.testCon())
- {
- return null;
- }
- Statement sql_statement = this.liveCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
- sql_statement.executeQuery(query);
- return sql_statement;
- }
- /**
- * 选择数据库
- */
- public void selectDb(String dbname) throws SQLException
- {
- this.testCon();
- this.execUpdate(" use `"+ dbname +"`; ");
- }
- /**
- * 预处理
- */
- private void preQuery() throws SQLException
- {
- this.execUpdate(" SET NAMES '"+ this.dbLang +"', character_set_client=binary, sql_mode='', wait_timeout="+ this.dbWaitTime +", interactive_timeout="+ this.dbWaitTime +" ; ");
- }
- /**
- * 关闭链接
- */
- public void close() throws SQLException
- {
- if(this.liveCon != null && !this.liveCon.isClosed() )
- {
- this.liveCon.close();
- }
- this.liveCon = null;
- }
- /**
- * 析放资源
- */
- public void finalize() {
- try {
- this.close();
- } catch(SQLException e) {
- e = null;
- }
- }
- }//end class
时间: 2024-10-29 02:18:54