Advertisement
Styczen007

Untitled

Nov 2nd, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import javax.servlet.ServletException;
  2. import javax.servlet.http.*;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.*;
  6.  
  7.  
  8. class DBHandler {
  9.     private String database = "";
  10.     private Connection conn;
  11.  
  12.     public DBHandler(String database){
  13.         this.database=database;
  14.     }
  15.  
  16.     public void setConn(Connection conn) {
  17.         this.conn = conn;
  18.     }
  19.  
  20.     public Connection getConn() {
  21.         return conn;
  22.     }
  23.  
  24.     public Connection getConnection(){
  25.         try {
  26.             Class.forName("com.mysql.jdbc.Driver");
  27.             conn = DriverManager.getConnection(database);
  28.  
  29.         } catch (SQLException e) {
  30.             e.printStackTrace();
  31.  
  32.         } catch (ClassNotFoundException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.         setConn(conn);
  37.     }
  38.  
  39.     public void retriveData() throws SQLException {
  40.         String sql="SELECT id,i,j FROM HData";
  41.         Statement statement = getConnection().createStatement();
  42.         ResultSet resultSet = statement.executeQuery(sql);
  43.         while (resultSet.next()){
  44.             int id  = resultSet.getInt("id");
  45.             int i = resultSet.getInt("i");
  46.             int j = resultSet.getInt("j");
  47.         }
  48.  
  49.     }
  50. }
  51.  
  52. public class Highway extends HttpServlet {
  53.     Connection conn;
  54.     DBHandler dbHandler;
  55.  
  56.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  57.         PrintWriter out = response.getWriter();
  58.         String database = "jdbc:mysql://localhost:3306/dbo?user=root&password=root";
  59.         dbHandler = new DBHandler(database);
  60.         dbHandler.getConnection();
  61.  
  62.     }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement