Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean connect(String SQL) {
- String connectionUrl = "jdbc:sqlserver://localhost:2175;" +
- "databaseName=TestConnection;integratedSecurity=True;";
- Connection con = null;
- Statement stm = null;
- ResultSet rs = null;
- boolean check = false;
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection(connectionUrl);
- stm = con.createStatement();
- if (SQL.contains("update") || SQL.contains("insert") || SQL.contains("delete") || SQL.contains("UPDATE") || SQL.contains("INSERT") || SQL.contains("DELETE")) {
- int clu = stm.executeUpdate(SQL);
- if (clu == 0)
- check = false;
- else
- check = true;
- }
- else {
- rs = stm.executeQuery(SQL);
- while (rs.next()) {
- System.out.println(rs.getString(1) + " | " + rs.getString(2));
- check = true;
- }
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- } finally {
- if (rs != null) try { rs.close(); } catch(Exception e) {}
- if (stm != null) try { stm.close(); } catch(Exception e) {}
- if (con != null) try { con.close(); } catch(Exception e) {}
- }
- return check;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement