Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class CustomQuery {
- static Connection conn = null;
- static Statement stmt = null;
- static ResultSet rs = null;
- public void getStringFromDatabase(){}
- public void getIntFromDatabase(){}
- public static void pendQuery(HttpServletRequest request, HttpServletResponse response) throws IOException{
- PrintWriter out = response.getWriter();
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String connectionUrl = "jdbc:mysql://localhost:3306/sys";
- String connectionUser = "root";
- String connectionPassword = "admin";
- conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT * FROM testowa");
- while (rs.next()) {
- int id = rs.getInt("idTestowa");
- String nazwa = rs.getString("nazwa");
- out.println("ID: " + id + ", Nazwa: " + nazwa);
- }
- } catch (Exception e) { e.printStackTrace(out);}
- finally {
- try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
- try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
- try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
- }
- conn = null;
- stmt = null;
- rs = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement