Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.*;
- import java.sql.*;
- @WebServlet("/DatabaseAccess")
- public class Databaseaccess extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // JDBC driver name and database URL
- final String DB_URL_with_SSL = "jdbc:mysql://127.0.0.1:3306/?user=root";
- // Database credentials
- final String USER = "root";
- final String PASS = "denise9955";
- // Set response content type
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String title = "Database Result";
- String docType = "<!doctype html>\n";
- out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body>\n" + "<h1>" + title
- + "</h1>\n");
- try {
- // Register JDBC driver
- Class.forName("com.mysql.cj.jdbc.Driver");
- // Open a connection with a try-with-resources: non c'e' bisogno di chiamare la funzione close su conn, verrà chiusa alla fine del body
- try (Connection conn = DriverManager.getConnection(DB_URL_with_SSL, USER, PASS)) {
- // Execute SQL query
- try (Statement stmt = conn.createStatement()) {
- String sql;
- sql = "SELECT nome, cognome FROM registration where login = 'fsina'";
- try (ResultSet rs = stmt.executeQuery(sql)) {
- // Extract data from result set
- while (rs.next()) {
- // Retrieve by column name
- String first = rs.getString("nome");
- String last = rs.getString("cognome");
- // Display values
- out.println("<h2>Ciao " + first + " ");
- out.println(last + "<h2>");
- }
- out.println("</body></html>");
- }
- }
- }
- } catch (SQLException | ClassNotFoundException e) {
- // Handle errors for JDBC
- e.printStackTrace();
- }
- }
- }
- MI DA' ERRORE SQL, "DATABASE NOT SELECTED"
- why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement