Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. public boolean connect(String SQL) {
  2. String connectionUrl = "jdbc:sqlserver://localhost:2175;" +
  3. "databaseName=TestConnection;integratedSecurity=True;";
  4. Connection con = null;
  5. Statement stm = null;
  6. ResultSet rs = null;
  7. boolean check = false;
  8. try {
  9. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  10. con = DriverManager.getConnection(connectionUrl);
  11. stm = con.createStatement();
  12. if (SQL.contains("update") || SQL.contains("insert") || SQL.contains("delete") || SQL.contains("UPDATE") || SQL.contains("INSERT") || SQL.contains("DELETE")) {
  13. int clu = stm.executeUpdate(SQL);
  14. if (clu == 0)
  15. check = false;
  16. else
  17. check = true;
  18. }
  19. else {
  20. rs = stm.executeQuery(SQL);
  21. while (rs.next()) {
  22. System.out.println(rs.getString(1) + " | " + rs.getString(2));
  23. check = true;
  24. }
  25. }
  26. } catch (ClassNotFoundException | SQLException e) {
  27. e.printStackTrace();
  28. } finally {
  29. if (rs != null) try { rs.close(); } catch(Exception e) {}
  30. if (stm != null) try { stm.close(); } catch(Exception e) {}
  31. if (con != null) try { con.close(); } catch(Exception e) {}
  32. }
  33.  
  34. return check;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement