Этот код имеет запись вставки в таблице mysql, но я хочу сравнить текстовые поля jsp/html String с столбцом mysql, который уже существует как логин. Это страница для входаКак сравнить две строки ввода из таблицы таблиц базы данных mysql.?
Две вещи, которые я знаю
- Чтобы изменить MySQL запрос
- Сравнить входной строки с данными столбцов MySQL Как я могу это сделать?
NewServlet.java
public class NewServlet1 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("username");
String abc=request.getParameter("password");
aaaa c1=new aaaa();
try{
String Sql="Select * from login where username='"+name+"' AND password='"+abc+"'";
c1.st.executeUpdate(Sql);
}
catch(SQLException ex)
{
out.println(ex);
}
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet1 at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
aaaa.java // комментарий этот класс для подключения к базе данных //
public class aaaa {
Connection c1 = null;
Statement st = null;
ResultSet rs = null;
aaaa(){
try {
Class.forName("com.mysql.jdbc.Driver");
c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
}
catch (Exception cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Couldn't connect: print out a stack trace and exit.");
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
}
try {
st = (Statement) c1.createStatement();
System.out.println("Statement Created Successfully");
} catch (SQLException se) {
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
se.printStackTrace();
}
if (c1 != null) {
System. out.println("Hooray! We connected to the database!");
} else {
System.out.println("We should never get here.");
}
}
}
Вы уязвимы для [SQL-инъекций] (http://bobby-tables.com) –
Почему вы не пытаетесь улучшить мой код? Я новичок в java-обучении, мой код вставляет данные в таблицу, но я хотите сравнить существующие данные таблицы с входной строкой, например, с именем пользователя или паролем. –
Потому что сначала вам нужно сделать домашнее задание. Изучите SQL, есть много доступных учебных пособий (например, бесплатный курс в [Code School] (https://www.codeschool.com/courses/try-sql)) - в частности, изучите команду SELECT и ее ' WHERE'. Здесь не здесь, чтобы научить вас основам. –