Guest User

Untitled

a guest
Sep 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. Query problems from Tomcat servlets to MSSQL Server Express 2012
  2. static {
  3. try {
  4. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  5. } catch (ClassNotFoundException e) {
  6. e.printStackTrace();
  7. }
  8. }
  9.  
  10. private final static String host = "ip-address:1433";
  11. private final static String database = "databasename";
  12. private final static String user = "user";
  13. private final static String passwd = "password";
  14.  
  15. public static void main(String[] args) {
  16. try {
  17. String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";integratedSecurity=true;";
  18. Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
  19. PreparedStatement pstm = con.prepareStatement("select * from sektor");
  20. ResultSet res = pstm.executeQuery();
  21. while(res.next())
  22. System.out.println("Sektor: " +res.getString("sektornavn"));
  23. } catch (SQLException e) {
  24. System.out.println(e.getMessage());
  25. }
  26. }
  27.  
  28. static {
  29. try {
  30. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. private final static String host = "ip-address:1433";
  37. private final static String database = "databasename";
  38. private final static String user = "user";
  39. private final static String passwd = "password";
  40.  
  41.  
  42. @Override
  43. public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  44. resp.setContentType("text/plain");
  45. resp.setCharacterEncoding("UTF-8");
  46. // Write response
  47. PrintWriter writer = resp.getWriter();
  48. try {
  49. String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";";
  50. writer.println("Connecting<br/>");
  51. Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
  52. writer.println("Preparing statement<br/>");
  53. PreparedStatement pstm = con.prepareStatement("select * from sektor");
  54. writer.println("Executing statement<br/>");
  55. ResultSet res = pstm.executeQuery();
  56. while(res.next())
  57. writer.println("Sektor: " +res.getString("sektornavn")+"<br/>");
  58. } catch (SQLException e) {
  59. writer.println(e.getMessage());
  60. }
  61. writer.flush();
  62. writer.close();
  63.  
  64. }
Add Comment
Please, Sign In to add comment