Java examples for java.sql:SqlServer
Exec Scalar sqlserver Database
//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 = "123@wsx"; public static Object ExecScalar(String cmdText) { ResultSet rs = getResultSet(cmdText); Object obj = buildScalar(rs); closeConnection(rs);//w w w . ja v a2s .c o m return obj; } public static Object ExecScalar(Connection conn, String cmdText) { ResultSet rs = getResultSet(conn, cmdText); Object obj = buildScalar(rs); closeEx(rs); return obj; } public static Object ExecScalar(String cmdText, Object... cmdParams) { ResultSet rs = getResultSet(cmdText, cmdParams); Object obj = buildScalar(rs); closeConnection(rs); return obj; } public static Object ExecScalar(Connection conn, String cmdText, Object... cmdParams) { ResultSet rs = getResultSet(conn, cmdText, cmdParams); Object obj = buildScalar(rs); closeEx(rs); return obj; } public static ResultSet getResultSet(String cmdText) { Statement stmt = getStatement(); if (stmt == null) { return null; } try { return stmt.executeQuery(cmdText); } catch (SQLException ex) { ex.printStackTrace(); closeConnection(stmt); } return null; } public static ResultSet getResultSet(Connection conn, String cmdText) { Statement stmt = getStatement(conn); if (stmt == null) { return null; } try { return stmt.executeQuery(cmdText); } catch (SQLException ex) { ex.printStackTrace(); close(stmt); } return null; } public static ResultSet getResultSet(String cmdText, Object... cmdParams) { PreparedStatement pstmt = getPreparedStatement(cmdText, cmdParams); if (pstmt == null) { return null; } try { return pstmt.executeQuery(); } catch (SQLException ex) { ex.printStackTrace(); closeConnection(pstmt); } return null; } public static ResultSet getResultSet(Connection conn, String cmdText, Object... cmdParams) { PreparedStatement pstmt = getPreparedStatement(conn, cmdText, cmdParams); if (pstmt == null) { return null; } try { return pstmt.executeQuery(); } catch (SQLException ex) { ex.printStackTrace(); close(pstmt); } return null; } public static Object buildScalar(ResultSet rs) { if (rs == null) { return null; } Object obj = null; try { if (rs.next()) { obj = rs.getObject(1); } } catch (SQLException ex) { ex.printStackTrace(); } return obj; } 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 closeEx(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).getStatement().close(); } else if (obj instanceof Connection) { ((Connection) obj).close(); } } catch (SQLException ex) { ex.printStackTrace(); } } 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 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; } } }