Java SQL Query ExecuteNoneQuerys(String cmdtext, Object[] parms)

Here you can find the source of ExecuteNoneQuerys(String cmdtext, Object[] parms)

Description

Execute None Querys

License

Open Source License

Declaration

public static int ExecuteNoneQuerys(String cmdtext, Object[] parms) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import java.sql.SQLException;

public class Main {
    public static String mysqlUrl = "";
    public static String user = "";
    public static String pwd = "";

    public static int ExecuteNoneQuerys(String cmdtext, Object[] parms) throws Exception {
        PreparedStatement pstmt = null;
        Connection conn = null;//from  www .  jav  a  2 s.  c o  m
        try {
            conn = getConnections();
            pstmt = conn.prepareStatement(cmdtext);
            prepareCommand(pstmt, parms);
            return pstmt.executeUpdate();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            throw new Exception("\n error:" + ex.toString());
        } finally {
            if (pstmt != null) {
                pstmt.clearParameters();
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }

        }
    }

    public static Connection getConnections() {
        try {
            return DriverManager.getConnection(mysqlUrl, user, pwd);
        } catch (SQLException ex) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    private static void prepareCommand(PreparedStatement pstmt, Object[] parms)
            throws SQLException, UnsupportedEncodingException {
        if (parms != null && parms.length > 0) {
            for (int i = 1; i < parms.length + 1; i++) {
                Object item = parms[i - 1];
                String typeName = item.getClass().getSimpleName();
                if (typeName.equals("String")) {
                    pstmt.setString(i, item.toString());
                } else if (typeName.equals("Integer")) {
                    pstmt.setInt(i, Integer.parseInt(item.toString()));
                } else if (typeName.equals("Date")) {
                    pstmt.setDate(i, Date.valueOf(item.toString()));
                } else {
                    pstmt.setObject(i, item);
                }
            }
        }
    }
}

Related

  1. createInQueryStatement(String query, long[] ids, Connection conn)
  2. executeCsvQuery(Connection csv, String csvTableName)
  3. executeDDL(String query, Connection connection)
  4. executeForResult(Connection conn, String query, String... args)
  5. executeQuery(Connection c, String query)
  6. ExecuteQuery(Connection conn, PreparedStatement preparedStatement, PreparedStatement lockStatement, String tablename)
  7. executeQuery(Connection connection)
  8. executeQuery(Connection connection, String query)