Exec Sql on sqlserver Database - Java java.sql

Java examples for java.sql:SqlServer

Description

Exec Sql on sqlserver Database

Demo Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

    private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    private static String url = "jdbc:jtds:sqlserver://192.168.81.16:55944;databaseName=ATDataBase";

    private static String user = "uapp_autotest";

    private static String password = "1qaz@wsx";

    public static int ExecSql(String cmdText) {
        Statement stmt = getStatement();
        if (stmt == null) {
            return -2;
        }//from w ww  . ja va 2s . com
        int i;
        try {
            i = stmt.executeUpdate(cmdText);
        } catch (SQLException ex) {
            ex.printStackTrace();
            i = -1;
        }
        closeConnection(stmt);
        return i;
    }

    public static int ExecSql(Connection conn, String cmdText) {
        Statement stmt = getStatement(conn);
        if (stmt == null) {
            return -2;
        }
        int i;
        try {
            i = stmt.executeUpdate(cmdText);
        } catch (SQLException ex) {
            ex.printStackTrace();
            i = -1;
        }
        close(stmt);
        return i;
    }

    public static int ExecSql(String cmdText, Object... cmdParams) {
        PreparedStatement pstmt = getPreparedStatement(cmdText, cmdParams);
        if (pstmt == null) {
            return -2;
        }
        int i;
        try {
            i = pstmt.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
            i = -1;
        }
        closeConnection(pstmt);
        return i;
    }

    public static int ExecSql(Connection conn, String cmdText,
            Object... cmdParams) {
        PreparedStatement pstmt = getPreparedStatement(conn, cmdText,
                cmdParams);
        if (pstmt == null) {
            return -2;
        }
        int i;
        try {
            i = pstmt.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
            i = -1;
        }
        close(pstmt);
        return i;
    }

    public static Statement getStatement() {
        Connection conn = getConnection();
        if (conn == null) {
            return null;
        }
        try {
            return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            // ?????????????,??????
        } catch (SQLException ex) {
            ex.printStackTrace();
            close(conn);
        }
        return null;
    }

    public static Statement getStatement(Connection conn) {
        if (conn == null) {
            return null;
        }
        try {
            return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            // ?????????????,??????
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private static void closeConnection(Object obj) {
        if (obj == null) {
            return;
        }
        try {
            if (obj instanceof Statement) {
                ((Statement) obj).getConnection().close();
            } else if (obj instanceof PreparedStatement) {
                ((PreparedStatement) obj).getConnection().close();
            } else if (obj instanceof ResultSet) {
                ((ResultSet) obj).getStatement().getConnection().close();
            } else if (obj instanceof Connection) {
                ((Connection) obj).close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private static void close(Object obj) {
        if (obj == null) {
            return;
        }
        try {
            if (obj instanceof Statement) {
                ((Statement) obj).close();
            } else if (obj instanceof PreparedStatement) {
                ((PreparedStatement) obj).close();
            } else if (obj instanceof ResultSet) {
                ((ResultSet) obj).close();
            } else if (obj instanceof Connection) {
                ((Connection) obj).close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static PreparedStatement getPreparedStatement(String cmdText,
            Object... cmdParams) {
        Connection conn = getConnection();
        if (conn == null) {
            return null;
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(cmdText,
                    ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            int i = 1;
            for (Object item : cmdParams) {
                pstmt.setObject(i, item);
                i++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            close(conn);
        }
        return pstmt;
    }

    public static PreparedStatement getPreparedStatement(Connection conn,
            String cmdText, Object... cmdParams) {
        if (conn == null) {
            return null;
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(cmdText,
                    ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            int i = 1;
            for (Object item : cmdParams) {
                pstmt.setObject(i, item);
                i++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            close(pstmt);
        }
        return pstmt;
    }

    public static Connection getConnection() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static Connection getConnection(String connecturl,
            String username, String psw) {
        url = connecturl;
        user = username;
        password = psw;
        return getConnection();
    }

    public static Connection getConnection(String connecturl) {
        url = connecturl;
        user = "";
        password = "";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            return DriverManager.getConnection(url);
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related Tutorials