Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. public class CustomQuery {
  15. static Connection conn = null;
  16. static Statement stmt = null;
  17. static ResultSet rs = null;
  18.  
  19. public void getStringFromDatabase(){}
  20. public void getIntFromDatabase(){}
  21.  
  22.  
  23. public static void pendQuery(HttpServletRequest request, HttpServletResponse response) throws IOException{
  24. PrintWriter out = response.getWriter();
  25. try {
  26. Class.forName("com.mysql.jdbc.Driver").newInstance();
  27. String connectionUrl = "jdbc:mysql://localhost:3306/sys";
  28. String connectionUser = "root";
  29. String connectionPassword = "admin";
  30. conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
  31. stmt = conn.createStatement();
  32. rs = stmt.executeQuery("SELECT * FROM testowa");
  33. while (rs.next()) {
  34. int id = rs.getInt("idTestowa");
  35. String nazwa = rs.getString("nazwa");
  36. out.println("ID: " + id + ", Nazwa: " + nazwa);
  37. }
  38. } catch (Exception e) { e.printStackTrace(out);}
  39. finally {
  40. try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
  41. try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
  42. try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
  43. }
  44. conn = null;
  45. stmt = null;
  46. rs = null;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement