Question
Why I can't see my data table. I am just seeing this when I run the server. -------- MySQL JDBC Connection Testing ------------ MySQL JDBC
Why I can't see my data table. I am just seeing this when I run the server.
-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
The code is below for MyServletDB.java
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/MyServletDB") public class MyServletDB extends HttpServlet { private static final long serialVersionUID = 1L; static String url = "jdbc:mysql:AWS:3306/myDB"; static String user = "newMysqlRemoteUser"; static String password = "mypassword"; static Connection connection = null;
public MyServletDB() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().println("-------- MySQL JDBC Connection Testing ------------ "); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("Where is your MySQL JDBC Driver?"); e.printStackTrace(); return; } response.getWriter().println("MySQL JDBC Driver Registered! "); connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } if (connection != null) { response.getWriter().println("You made it, take control your database now! "); } else { System.out.println("Failed to make connection!"); } try { String selectSQL = "SELECT * FROM myTable WHERE MYUSER LIKE ?"; String theUserName = "user%"; response.getWriter().println(selectSQL + " "); response.getWriter().println("------------------------------------------ "); PreparedStatement preparedStatement = connection.prepareStatement(selectSQL); preparedStatement.setString(1, theUserName); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String id = rs.getString("ID"); String username = rs.getString("MYUSER"); String email = rs.getString("EMAIL"); String phone = rs.getString("PHONE"); response.getWriter().append("USER ID: " + id + ", "); response.getWriter().append("USER NAME: " + username + ", "); response.getWriter().append("USER EMAIL: " + email + ", "); response.getWriter().append("USER PHONE: " + phone + " "); } } catch (SQLException e) { e.printStackTrace(); } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started