Não consigo carregar a tabela da base de dados conforme seleciono um nome no botão search.
Não achei o problema, poderiam me ajudar
index.jsp
```
<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>search</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form class="form-inline" method="post" action="search.jsp">
<input type="text" name="employee_n" class="form-control"
placeholder="Search name..">
<button type="submit" name="save" class="btn btn-primary">Search</button>
</form>
</div>
</body>
</html>
```
search.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "userdetails";
String userid = "root";
String password = "123456";
String employee_n=request.getParameter("employee_n");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SEARCH</title>
</head>
<body>
<h1>Search Data</h1>
<table border="1">
<tr>
<td>Id</td>
<td>Name</td>
<td>Email </td>
<td>Dpt</td>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select * from employee where emp_name like '%"+employee_n+"%' " ;
System.out.println(employee_n);
//System.out.println(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("id") %></td>
<td><%=resultSet.getString("emp_name") %></td>
<td><%=resultSet.getString("emp_dept") %></td>
<td><%=resultSet.getString("email") %></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
```
That's my data in MySQL
CREATE DATABASE userdetails;
```
use userdetails;
create table employee(
id int auto_increment,
emp_name varchar(100),
emp_dept varchar(100),
email varchar(100),
primary key(id)
);
insert into employee (emp_name, emp_dept, email)
values
("baner","engenharia", "baner@ig.com");
```
Tags
Faz alguns anos q eu n uso jsp, mas pq vc está fazendo a conexão do banco de dado em um arquivo jsp?
Foi um exemplo que achei na net, estou iniciando os estudos
Existe o banco de dados userdetails?
Esse banco tem a tabela employee?
Qual erro que ele exibe?
Sim existe, este
CREATE DATABASE userdetails;
use userdetails;
create table employee(
id int auto_increment,
emp_name varchar(100),
emp_dept varchar(100),
email varchar(100),
primary key(id)
);
no campo index eu passei o parâmetro employee_n
No campo search dei um print e ele realmente chegou, mas o SQL o coloca como vazio
String sql =“select * from employee where emp_name like '%”+employee_n+"%’ " ;
System.out.println(employee_n);
employee_n marcel tem no BD e entrei com ele no input do index.jsp
console:
INFO: Server startup in 29132 ms
marcel
java.lang.NullPointerException
at org.apache.jsp.search2_jsp._jspService(search2_jsp.java:173)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
mysql> use userdetails;
Database changed
mysql> show tables;
±----------------------+
| Tables_in_userdetails |
±----------------------+
| employee |
±----------------------+
1 row in set (0.00 sec)
mysql> select * from employee;
±—±-------------±----------------±--------------+
| id | emp_name | emp_dept | email |
±—±-------------±----------------±--------------+
| 1 | marcel | ti | mm@gmail.com |
| 2 | steve rogers | desenvolvimento | sr@bol.com.br |
| 3 | baner | engenharia | baner@ig.com |
±—±-------------±----------------±--------------+
3 rows in set (0.00 sec)
<input type="text" name="employee_n" class="form-control"
placeholder="Search name..">
chegou a colocar o id desse campo como “employee_n”
sim, aqui
form class="form-inline" method="post" action="search.jsp">
<input type="text" name="employee_n" class="form-control"
placeholder="Search name..">
<button type="submit" name="save" class="btn btn-primary">Se
Ele está até chegando, mas na sql fica nulo
Consegui, faltpou fazer a ArrayList par carregar
<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”
pageEncoding=“ISO-8859-1”%>
<%@page import= "java.util.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "userdetails";
String userid = "root";
String password = "123456";
String employee_n=request.getParameter("employee_n");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement st = null;
ResultSet rs = null;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SEARCH 2</title>
</head>
<body>
<h1>Search Data</h1>
<table border="1">
<tr>
<td>Id</td>
<td>Name</td>
<td>Email </td>
<td>Dpt</td>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
st=connection.createStatement();
ArrayList al=null;
ArrayList emp_list =new ArrayList();
String sql ="select * from employee where emp_name like '%"+employee_n+"%' " ;
System.out.println(employee_n);
System.out.println(sql);
st = connection.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("emp_name") %></td>
<td><%=rs.getString("emp_dept") %></td>
<td><%=rs.getString("email") %></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>