- package com.tool;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import java.sql.Statement;
- //这是一个工具类实现与数据库的连接
- public class JDBConnection {
- String sd = "sun.jdbc.odbc.JdbcOdbcDriver";
- String sc = "jdbc:odbc:db_Client";
- ResultSet rs = null;
- Connection conect = null;
- public JDBConnection() {
- try {
- Class.forName(sd);
- }
- catch (java.lang.ClassNotFoundException e) {
- System.err.println(e.getMessage());
- }
- }
- public ResultSet executeQuery(String sql) {
- try {
- conect = DriverManager.getConnection(sc);
- Statement stmt = conect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(sql);
- }
- catch (SQLException ex) {
- System.err.println(ex.getMessage());
- }
- return rs;
- }
- public int executeUpdate(String sql) {
- int result = 0;
- try {
- conect = DriverManager.getConnection(sc);
- Statement stmt = conect.createStatement();
- result = stmt.executeUpdate(sql);
- }
- catch (SQLException er) {
- System.err.println(er.getMessage());
- }
- return result;
- }
- public void close() {
- try {
- if (conect != null) {
- conect.close();
- }
- }
- catch (Exception e) {
- System.out.println(e);
- }
- }
- //设置提交
- public void setAutoCommit(boolean flag) {
- try {
- conect.setAutoCommit(flag);
- }
- catch (Exception exception) {
- System.out.println("Exception in set autoCommit :" + exception.getMessage());
- }
- }
- //执行提交
- public void commit() {
- try {
- conect.commit();
- }
- catch (Exception exception) {
- System.out.println("Exception in commit :" + exception.getMessage());
- }
- }
- //执行回滚
- public void rollback() {
- try {
- conect.rollback();
- }
- catch (Exception exception) {
- System.out.println("Exception in rollback :" + exception.getMessage());
- }
- }
- //将sql语句中的?号转为相应的参数 ,其中参数为字符串型的
- public String editSqlCode(String sql, String str) {
- try {
- // 截取从0到第一个问号的字符串加参数str,然后在加上第一问号以后的字符串
- sql = sql.substring(0, sql.indexOf("?")) + str +
- sql.substring(sql.indexOf("?") + 1);
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return sql;
- }
- //将sql语句中的?号转为相应的参数 ,其中参数为长整型的
- public String editSqlCode(String sql, Integer str) {
- try {
- sql = sql.substring(0, sql.indexOf("?")) + str +
- sql.substring(sql.indexOf("?") + 1);
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return sql;
- }
- //将sql语句中的?号转为相应的参数 ,其中参数为整型的
- public String editSqlCode(String sql, int str) { //提取SQL语句,并把(int str)传出来的值来代替?
- try {
- sql = sql.substring(0, sql.indexOf("?")) + str +
- sql.substring(sql.indexOf("?") + 1);
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return sql;
- }
- //将sql语句中的?号转为相应的参数 ,其中参数为整型的
- public String editSqlCode(String sql, Float str) { //提取SQL语句,并把(Float str)传出来的值来代替?
- try {
- sql = sql.substring(0, sql.indexOf("?")) + str +
- sql.substring(sql.indexOf("?") + 1);
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return sql;
- }
- }