SHOW:
|
|
- or go back to the newest paste.
| 1 | /* | |
| 2 | * To change this license header, choose License Headers in Project Properties. | |
| 3 | * To change this template file, choose Tools | Templates | |
| 4 | * and open the template in the editor. | |
| 5 | */ | |
| 6 | package Utils; | |
| 7 | ||
| 8 | import java.io.Serializable; | |
| 9 | import java.sql.Connection; | |
| 10 | import java.sql.DriverManager; | |
| 11 | import java.sql.PreparedStatement; | |
| 12 | import java.sql.ResultSet; | |
| 13 | import java.sql.ResultSetMetaData; | |
| 14 | import java.sql.SQLException; | |
| 15 | import java.sql.Statement; | |
| 16 | import java.util.ArrayList; | |
| 17 | import java.util.List; | |
| 18 | ||
| 19 | /** | |
| 20 | * | |
| 21 | * @author TAM LONG | |
| 22 | */ | |
| 23 | public class JDBCUtil implements Serializable {
| |
| 24 | ||
| 25 | public JDBCUtil() {
| |
| 26 | } | |
| 27 | ||
| 28 | public String createConnectionString(String dbName, String user, String pass) {
| |
| 29 | String conString = "jdbc:sqlserver://PHUCNT;databaseName=" | |
| 30 | + dbName + ";user=" + user + ";password=" + pass; | |
| 31 | return conString; | |
| 32 | } | |
| 33 | ||
| 34 | public int CUDQuery(String conString, String sqlQuery) {
| |
| 35 | Connection con = null; | |
| 36 | Statement stmt = null; | |
| 37 | int rows = 0; | |
| 38 | try {
| |
| 39 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
| |
| 40 | con = DriverManager.getConnection(conString); | |
| 41 | stmt = con.createStatement(); | |
| 42 | rows = stmt.executeUpdate(sqlQuery); | |
| 43 | return rows; | |
| 44 | } catch (Exception ex) {
| |
| 45 | ex.printStackTrace(); | |
| 46 | } finally {
| |
| 47 | try {
| |
| 48 | stmt.close(); | |
| 49 | con.close();; | |
| 50 | } catch (Exception ex) {
| |
| 51 | ex.printStackTrace(); | |
| 52 | } | |
| 53 | } | |
| 54 | return 0; | |
| 55 | } | |
| 56 | ||
| 57 | public boolean insert(String conString, String query, Object[] params) {
| |
| 58 | Connection con = null; | |
| 59 | PreparedStatement pstm = null; | |
| 60 | try {
| |
| 61 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
| |
| 62 | con = DriverManager.getConnection(conString); | |
| 63 | pstm = con.prepareStatement(query); | |
| 64 | for (int i = 0; i < params.length; i++) {
| |
| 65 | pstm.setObject(i + 1, params[i]); | |
| 66 | } | |
| 67 | int result = pstm.executeUpdate(); | |
| 68 | if (result > 0) {
| |
| 69 | return true; | |
| 70 | } | |
| 71 | } catch (Exception e) {
| |
| 72 | e.printStackTrace(); | |
| 73 | } finally {
| |
| 74 | try {
| |
| 75 | if (pstm != null) {
| |
| 76 | pstm.close(); | |
| 77 | } | |
| 78 | if (con != null) {
| |
| 79 | con.close(); | |
| 80 | } | |
| 81 | } catch (Exception e) {
| |
| 82 | e.printStackTrace(); | |
| 83 | } | |
| 84 | } | |
| 85 | return false; | |
| 86 | } | |
| 87 | ||
| 88 | public List<Object[]> getAll(String conString, String sql) throws SQLException {
| |
| 89 | Connection con = null; | |
| 90 | PreparedStatement pstm = null; | |
| 91 | ResultSet rs = null; | |
| 92 | try {
| |
| 93 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
| |
| 94 | con = DriverManager.getConnection(conString); | |
| 95 | pstm = con.prepareStatement(sql); | |
| 96 | rs = pstm.executeQuery(); | |
| 97 | ResultSetMetaData rsm = rs.getMetaData(); | |
| 98 | int cols = rsm.getColumnCount(); | |
| 99 | List<Object[]> list = new ArrayList<Object[]>(); | |
| 100 | while (rs.next()) {
| |
| 101 | Object[] row = new Object[cols]; | |
| 102 | for (int i = 0; i < cols; i++) {
| |
| 103 | row[i] = rs.getObject(i + 1); | |
| 104 | } | |
| 105 | list.add(row); | |
| 106 | } | |
| 107 | return list; | |
| 108 | ||
| 109 | } catch (Exception e) {
| |
| 110 | e.printStackTrace(); | |
| 111 | } finally {
| |
| 112 | try {
| |
| 113 | if (rs != null) {
| |
| 114 | rs.close(); | |
| 115 | } | |
| 116 | ||
| 117 | if (pstm != null) {
| |
| 118 | pstm.close(); | |
| 119 | } | |
| 120 | ||
| 121 | if (con != null) {
| |
| 122 | con.close(); | |
| 123 | } | |
| 124 | } catch (Exception e) {
| |
| 125 | e.printStackTrace(); | |
| 126 | } | |
| 127 | } | |
| 128 | return null; | |
| 129 | } | |
| 130 | ||
| 131 | } |