PreparedStatement 输出SQL语句

该代码示例展示了一个Java程序,它使用PreparedStatement与Hive数据库建立连接并执行SQL查询。特别是,它创建了一个LoggableStatement类来包装预编译的SQL语句,以便在执行时保存参数值并生成完整可读的SQL日志。程序还包含了错误处理和资源关闭的逻辑。
摘要由CSDN通过智能技术生成

调用

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet res = null;  
        try {
			con =  DriverManager.getConnection("jdbc:hive2://192.168.30.45:10000", "root", "");
	        String sql = "show tables"; // 查询表
	        sql = "select * from IT_BI_PRD_EIS.TE_PM_DASHBD_INST order by Refresh_Ts desc"; // 实际
//	        
	        String sqlInsert =  "INSERT INTO IT_BI_PRD_EIS.TE_PM_DASHBD_INST_BGT"
										 + " VALUES(?,?,?,'2099-12-31','MONTH','SUM','0','1','','HKD','BUDGET',?,'FIN_BGT',?,?,?)";
									 
									 int columnIndex = cell.getColumnIndex();
//									 pStmt = con.prepareStatement( sqlInsert2 );
									pStmt = new LoggableStatement(con, sqlInsert); 
									 System.out.println(cellIndex + ",mmmss-------" + sqlInsert2);
									 pStmt.setString(1, cellID );
									 pStmt.setString(2, cellID+"."+cellYear +"M"+ (columnIndex<10?("0"+columnIndex):columnIndex) );
									 pStmt.setString(3, cellYear+"-01-01" );
									 pStmt.setDouble(4, cell.getNumericCellValue() );
									 pStmt.setString(5, date1 );
									 pStmt.setString(6, date1 );
									 pStmt.setString(7, date0 );
									 
									 System.out.println("目标------- SQL: "+((LoggableStatement)pStmt).getQueryString());
									 int i = pStmt.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            if (pStmt != null) {
                try {
                	pStmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                pStmt = null;
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                con = null;
            }
        }
        
	}

------ 工具类 ------

package com.xxx.xxxHiveHelper.util;

import java.io.InputStream;    
import java.io.Reader;    
import java.math.BigDecimal;    
import java.net.URL;    
import java.sql.Array;    
import java.sql.Blob;    
import java.sql.Clob;    
import java.sql.Connection;    
import java.sql.Date;    
import java.sql.NClob;  
import java.sql.ParameterMetaData;    
import java.sql.PreparedStatement;    
import java.sql.Ref;    
import java.sql.ResultSet;    
import java.sql.ResultSetMetaData;    
import java.sql.RowId;  
import java.sql.SQLException;    
import java.sql.SQLWarning;    
import java.sql.SQLXML;  
import java.sql.Time;    
import java.sql.Timestamp;    
import java.util.ArrayList;    
import java.util.Calendar;        
 
public class LoggableStatement implements PreparedStatement {    
    
    /** used for storing parameter values needed for producing log */    
    private ArrayList parameterValues;    
    
    /** the query string with question marks as parameter placeholders */    
    private String sqlTemplate;    
    
    /** a statement created from a real database connection */    
    private PreparedStatement wrappedStatement;    
    
    public LoggableStatement(Connection connection, String sql)    
            throws SQLException {    
        // use connection to make a prepared statement    
        wrappedStatement = connection.prepareStatement(sql);    
        sqlTemplate = sql;    
        parameterValues = new ArrayList();    
    }    
    
    private void saveQueryParamValue(int position, Object obj) {    
        String strValue;    
        if (obj instanceof String || obj instanceof Date) {    
            // if we have a String, include '' in the saved value    
            strValue = "'" + obj + "'";    
        } else {    
            if (obj == null) {    
                // convert null to the string null    
                strValue = "null";    
            } else {    
                // unknown object (includes all Numbers), just call toString    
                strValue = obj.toString();    
            }    
        }    
        // if we are setting a position larger than current size of    
        // parameterValues, first make it larger    
        while (position >= parameterValues.size()) {    
    
            parameterValues.add(null);    
        }    
        // save the parameter    
        parameterValues.set(position, strValue);    
    }    
    
    // 这一步是对ArrayList与sql进行处理,输出完整的sql语句    
    public String getQueryString() {    
        int len = sqlTemplate.length();    
        StringBuffer t = new StringBuffer(len * 2);    
    
        if (parameterValues != null) {    
            int i = 1, limit = 0, base = 0;    
    
            while ((limit = sqlTemplate.indexOf('?', limit)) != -1) {    
                t.append(sqlTemplate.substring(base, limit));    
                t.append(parameterValues.get(i));    
                i++;    
                limit++;    
                base = limit;    
            }    
            if (base < len) {    
                t.append(sqlTemplate.substring(base));    
            }    
        }    
        return t.toString();    
    }    
    
    public void addBatch() throws SQLException {    
        wrappedStatement.addBatch();    
    }    
    
    public void clearParameters() throws SQLException {    
        wrappedStatement.clearParameters();    
    }    
    
    public boolean execute() throws SQLException {    
        return wrappedStatement.execute();    
    }    
    
    public ResultSet executeQuery() throws SQLException {    
        return wrappedStatement.executeQuery();    
    }    
    
    public int executeUpdate() throws SQLException {    
        return wrappedStatement.executeUpdate();    
    }    
    
    public ResultSetMetaData getMetaData() throws SQLException {    
        return wrappedStatement.getMetaData();    
    }    
    
    public ParameterMetaData getParameterMetaData() throws SQLException {    
        return wrappedStatement.getParameterMetaData();    
    }    
    
    public void setArray(int i, Array x) throws SQLException {    
        wrappedStatement.setArray(i, x);    
        saveQueryParamValue(i, x);    
    }    
    
    public void setAsciiStream(int parameterIndex, InputStream x, int length)    
            throws SQLException {    
        wrappedStatement.setAsciiStream(parameterIndex, x, length);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setBigDecimal(int parameterIndex, BigDecimal x)    
            throws SQLException {    
        wrappedStatement.setBigDecimal(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setBinaryStream(int parameterIndex, InputStream x, int length)    
            throws SQLException {    
        wrappedStatement.setBinaryStream(parameterIndex, x, length);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setBlob(int i, Blob x) throws SQLException {    
        wrappedStatement.setBlob(i, x);    
        saveQueryParamValue(i, x);    
    }    
    
    public void setBoolean(int parameterIndex, boolean x) throws SQLException {    
        wrappedStatement.setBoolean(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Boolean(x));    
    }    
    
    public void setByte(int parameterIndex, byte x) throws SQLException {    
        wrappedStatement.setByte(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Byte(x));    
    }    
    
    public void setBytes(int parameterIndex, byte[] x) throws SQLException {    
        wrappedStatement.setBytes(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setCharacterStream(int parameterIndex, Reader reader, int length)    
            throws SQLException {    
        wrappedStatement.setCharacterStream(parameterIndex, reader, length);    
        saveQueryParamValue(parameterIndex, reader);    
    }    
    
    public void setClob(int i, Clob x) throws SQLException {    
        wrappedStatement.setClob(i, x);    
        saveQueryParamValue(i, x);    
    }    
    
    public void setDate(int parameterIndex, Date x) throws SQLException {    
        wrappedStatement.setDate(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setDate(int parameterIndex, Date x, Calendar cal)    
            throws SQLException {    
        wrappedStatement.setDate(parameterIndex, x, cal);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setDouble(int parameterIndex, double x) throws SQLException {    
        wrappedStatement.setDouble(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Double(x));    
    }    
    
    public void setFloat(int parameterIndex, float x) throws SQLException {    
        wrappedStatement.setFloat(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Float(x));    
    }    
    
    public void setInt(int parameterIndex, int x) throws SQLException {    
        wrappedStatement.setInt(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Integer(x));    
    }    
    
    public void setLong(int parameterIndex, long x) throws SQLException {    
        wrappedStatement.setLong(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Long(x));    
    }    
    
    public void setNull(int parameterIndex, int sqlType) throws SQLException {    
        wrappedStatement.setNull(parameterIndex, sqlType);    
        saveQueryParamValue(parameterIndex, new Integer(sqlType));    
    }    
    
    public void setNull(int paramIndex, int sqlType, String typeName)    
            throws SQLException {    
        wrappedStatement.setNull(paramIndex, sqlType, typeName);    
        saveQueryParamValue(paramIndex, new Integer(sqlType));    
    }    
    
    public void setObject(int parameterIndex, Object x) throws SQLException {    
        wrappedStatement.setObject(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setObject(int parameterIndex, Object x, int targetSqlType)    
            throws SQLException {    
        wrappedStatement.setObject(parameterIndex, x, targetSqlType);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setObject(int parameterIndex, Object x, int targetSqlType,    
            int scale) throws SQLException {    
        wrappedStatement.setObject(parameterIndex, x, targetSqlType, scale);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setRef(int i, Ref x) throws SQLException {    
        wrappedStatement.setRef(i, x);    
        saveQueryParamValue(i, x);    
    }    
    
    public void setShort(int parameterIndex, short x) throws SQLException {    
        wrappedStatement.setShort(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, new Short(x));    
    }    
    
    public void setString(int parameterIndex, String x) throws SQLException {    
        wrappedStatement.setString(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setTime(int parameterIndex, Time x) throws SQLException {    
        wrappedStatement.setTime(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setTime(int parameterIndex, Time x, Calendar cal)    
            throws SQLException {    
        wrappedStatement.setTime(parameterIndex, x, cal);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setTimestamp(int parameterIndex, Timestamp x)    
            throws SQLException {    
        wrappedStatement.setTimestamp(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)    
            throws SQLException {    
        wrappedStatement.setTimestamp(parameterIndex, x, cal);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setURL(int parameterIndex, URL x) throws SQLException {    
        wrappedStatement.setURL(parameterIndex, x);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void setUnicodeStream(int parameterIndex, InputStream x, int length)    
            throws SQLException {    
        wrappedStatement.setUnicodeStream(parameterIndex, x, length);    
        saveQueryParamValue(parameterIndex, x);    
    }    
    
    public void addBatch(String sql) throws SQLException {    
        wrappedStatement.addBatch(sql);    
    }    
    
    public void cancel() throws SQLException {    
        wrappedStatement.cancel();    
    }    
    
    public void clearBatch() throws SQLException {    
        wrappedStatement.clearBatch();    
    }    
    
    public void clearWarnings() throws SQLException {    
        wrappedStatement.clearWarnings();    
    }    
    
    public void close() throws SQLException {    
        wrappedStatement.close();    
    }    
    
    public boolean execute(String sql) throws SQLException {    
        return wrappedStatement.execute(sql);    
    }    
    
    public boolean execute(String sql, int autoGeneratedKeys)    
            throws SQLException {    
        return wrappedStatement.execute(sql, autoGeneratedKeys);    
    }    
    
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {    
        return wrappedStatement.execute(sql, columnIndexes);    
    }    
    
    public boolean execute(String sql, String[] columnNames)    
            throws SQLException {    
        return wrappedStatement.execute(sql, columnNames);    
    }    
    
    public int[] executeBatch() throws SQLException {    
        return wrappedStatement.executeBatch();    
    }    
    
    public ResultSet executeQuery(String sql) throws SQLException {    
        return wrappedStatement.executeQuery(sql);    
    }    
    
    public int executeUpdate(String sql) throws SQLException {    
        return wrappedStatement.executeUpdate(sql);    
    }    
    
    public int executeUpdate(String sql, int autoGeneratedKeys)    
            throws SQLException {    
        return wrappedStatement.executeUpdate(sql, autoGeneratedKeys);    
    }    
    
    public int executeUpdate(String sql, int[] columnIndexes)    
            throws SQLException {    
        return wrappedStatement.executeUpdate(sql, columnIndexes);    
    }    
    
    public int executeUpdate(String sql, String[] columnNames)    
            throws SQLException {    
        return wrappedStatement.executeUpdate(sql, columnNames);    
    }    
    
    public Connection getConnection() throws SQLException {    
        return wrappedStatement.getConnection();    
    }    
    
    public int getFetchDirection() throws SQLException {    
        return wrappedStatement.getFetchDirection();    
    }    
    
    public int getFetchSize() throws SQLException {    
        return wrappedStatement.getFetchSize();    
    }    
    
    public ResultSet getGeneratedKeys() throws SQLException {    
        return wrappedStatement.getGeneratedKeys();    
    }    
    
    public int getMaxFieldSize() throws SQLException {    
        return wrappedStatement.getMaxFieldSize();    
    }    
    
    public int getMaxRows() throws SQLException {    
        return wrappedStatement.getMaxRows();    
    }    
    
    public boolean getMoreResults() throws SQLException {    
        return wrappedStatement.getMoreResults();    
    }    
    
    public boolean getMoreResults(int current) throws SQLException {    
        return wrappedStatement.getMoreResults(current);    
    }    
    
    public int getQueryTimeout() throws SQLException {    
        return wrappedStatement.getQueryTimeout();    
    }    
    
    public ResultSet getResultSet() throws SQLException {    
        return wrappedStatement.getResultSet();    
    }    
    
    public int getResultSetConcurrency() throws SQLException {    
        return wrappedStatement.getResultSetConcurrency();    
    }    
    
    public int getResultSetHoldability() throws SQLException {    
        return wrappedStatement.getResultSetHoldability();    
    }    
    
    public int getResultSetType() throws SQLException {    
        return wrappedStatement.getResultSetType();    
    }    
    
    public int getUpdateCount() throws SQLException {    
        return wrappedStatement.getUpdateCount();    
    }    
    
    public SQLWarning getWarnings() throws SQLException {    
        return wrappedStatement.getWarnings();    
    }    
    
    public void setCursorName(String name) throws SQLException {    
        wrappedStatement.setCursorName(name);    
    }    
    
    public void setEscapeProcessing(boolean enable) throws SQLException {    
        wrappedStatement.setEscapeProcessing(enable);    
    }    
    
    public void setFetchDirection(int direction) throws SQLException {    
        wrappedStatement.setFetchDirection(direction);    
    }    
    
    public void setFetchSize(int rows) throws SQLException {    
        wrappedStatement.setFetchSize(rows);    
    }    
    
    public void setMaxFieldSize(int max) throws SQLException {    
        wrappedStatement.setMaxFieldSize(max);    
    }    
    
    public void setMaxRows(int max) throws SQLException {    
        wrappedStatement.setMaxFieldSize(max);    
    }    
    
    public void setQueryTimeout(int seconds) throws SQLException {    
        wrappedStatement.setQueryTimeout(seconds);    
    }  
  
    public void setAsciiStream(int parameterIndex, InputStream x)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setAsciiStream(int parameterIndex, InputStream x, long length)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setBinaryStream(int parameterIndex, InputStream x)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setBinaryStream(int parameterIndex, InputStream x, long length)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setBlob(int parameterIndex, InputStream inputStream)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setBlob(int parameterIndex, InputStream inputStream, long length)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setCharacterStream(int parameterIndex, Reader reader)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setCharacterStream(int parameterIndex, Reader reader,  
            long length) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setClob(int parameterIndex, Reader reader) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setClob(int parameterIndex, Reader reader, long length)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNCharacterStream(int parameterIndex, Reader value)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNCharacterStream(int parameterIndex, Reader value,  
            long length) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNClob(int parameterIndex, NClob value) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNClob(int parameterIndex, Reader reader) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNClob(int parameterIndex, Reader reader, long length)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setNString(int parameterIndex, String value)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setRowId(int parameterIndex, RowId x) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void setSQLXML(int parameterIndex, SQLXML xmlObject)  
            throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public boolean isClosed() throws SQLException {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    public boolean isPoolable() throws SQLException {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    public void setPoolable(boolean poolable) throws SQLException {  
        // TODO Auto-generated method stub  
          
    }  
  
    public boolean isWrapperFor(Class<?> iface) throws SQLException {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    public <T> T unwrap(Class<T> iface) throws SQLException {  
        // TODO Auto-generated method stub  
        return null;  
    }

	@Override
	public void closeOnCompletion() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isCloseOnCompletion() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}    
} 
以下是使用Statement和PreparedStatement执行SQL语句的代码示例: 1. 使用Statement执行SQL语句[^1]: ```java import java.sql.*; public class StatementExample { public static void main(String[] args) { try { // 建立数据库连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建Statement对象 Statement stmt = conn.createStatement(); // 执行SQL查询语句 String sql = "SELECT * FROM mytable"; ResultSet rs = stmt.executeQuery(sql); // 处理查询结果 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 2. 使用PreparedStatement执行SQL语句[^2]: ```java import java.sql.*; public class PreparedStatementExample { public static void main(String[] args) { try { // 建立数据库连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建PreparedStatement对象 String sql = "SELECT * FROM mytable WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setInt(1, 1); // 执行查询 ResultSet rs = pstmt.executeQuery(); // 处理查询结果 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } // 关闭连接 rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值