JDBC查询数据返回给jsp页面(注解方式完成)

来源:互联网 发布:手机有淘宝助理吗 编辑:程序博客网 时间:2024/05/17 04:36
所需要jar包:

mysql图表

eclipse框架

input.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form id="myForm">
ID:<input type="text" name="id" /> <input type="button"
value="submit" id="btn">
</form>
</body>
<script>
var oBtn = document.getElementById("btn");
oBtn.onclick = function() {//js点击事件
var oInput = document.getElementsByTagName("input")[0];
var id = oInput.value;
location.href = "/jdbc/Servlet?id=" + id;
}
/* oBtn.onclick = function(){
document.getElementById("myForm").submit();
} */
</script>
</html>
return.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>姓名:${map.Id}</div>
<div>年龄:${map.Name}</div>
<div>年龄:${map.Sex}</div>
<div>年龄:${map.Age}</div>
</body>
</html>
Jdbc.java
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class Jdbc {
// 声明Connection对象
java.sql.Connection con;
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/testmysql";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "root";
// 结果集
java.sql.ResultSet rs;
java.sql.Statement statement = null;
public Map<String, Object> get(String userId) {
// 遍历查询结果集
try {
// 加载驱动程序
Class.forName(driver);
// 1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url, user, password);
// 2.创建statement类对象,用来执行SQL语句!!
statement = con.createStatement();
// 要执行的SQL语句
String sql = "SELECT * FROM student WHERE Id=" + userId;
// 3.ResultSet类,用来存放获取的结果集!!
rs = statement.executeQuery(sql);
Map<String, Object> m = null;
while (rs.next()) {
String Id = rs.getString("Id");
String Name = rs.getString("Name");
String Sex = rs.getString("Sex");
String Age = rs.getString("Age");
m = new HashMap<String, Object>();
m.put("Id", Id);
m.put("Name", Name);
m.put("Sex", Sex);
m.put("Age", Age);
}
return m;
} catch (Exception e) {
System.out.println("Sorry,can`t find the Driver!");
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
Servlet.java
package com.jdbc;
import java.io.IOException;
import java.util.Map;
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("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String userId = request.getParameter("id");
Map<String, Object> map = new Jdbc().get(userId);
request.setAttribute("map", map);
request.getRequestDispatcher("/return.jsp").forward(request,response);
}
}
测试结果


主要是主要这里面的数据怎么传的,先是从input获得查询条件——提交到servlet中——jdbc调取mysql中的数据——return反馈给用户查询结果


原创粉丝点击