package com.yfw;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tutorialspoint.Student;
import com.tutorialspoint.StudentJDBCTemplate;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class DatabaseAccess
*/
@WebServlet("/DatabaseAccess")
public class DatabaseAccess extends HttpServlet {
private static final long serialVersionUID = 1L;
// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "123456";
/**
* @see HttpServlet#HttpServlet()
*/
public DatabaseAccess() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
/*
System.out.println("------Records Creation--------" );
studentJDBCTemplate.create("Zara", 11);
studentJDBCTemplate.create("Nuha", 2);
studentJDBCTemplate.create("Ayan", 15);
System.out.println("------Listing Multiple Records--------" );
*/
response.setContentType("text/json; charset=utf-8");
PrintWriter out = response.getWriter();
JSONArray jsonarray = new JSONArray();
JSONObject jsonobj = new JSONObject();
List<Student> students = studentJDBCTemplate.listStudents();
for (Student record : students) {
/*
System.out.print("ID : " + record.getId() );
System.out.print(", Name : " + record.getName() );
System.out.println(", Age : " + record.getAge());
*/
jsonobj.put("ID", record.getId());
jsonobj.put("Name", record.getName());
jsonobj.put("Age", record.getAge());
jsonarray.add(jsonobj);
}
out = response.getWriter();
out.println(jsonarray);
/*
System.out.println("----Updating Record with ID = 2 -----" );
studentJDBCTemplate.update(2, 20);
System.out.println("----Listing Record with ID = 2 -----" );
Student student = studentJDBCTemplate.getStudent(2);
System.out.print("ID : " + student.getId() );
System.out.print(", Name : " + student.getName() );
System.out.println(", Age : " + student.getAge());
*/
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Spring+Servlet读取数据库返回JSON数据(源码).zip (52个子文件)
dbtest
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 35B
lib
commons-collections-3.1.jar 546KB
spring-instrument-tomcat-4.1.6.RELEASE.jar 10KB
spring-oxm-4.1.6.RELEASE.jar 80KB
spring-beans-4.1.6.RELEASE.jar 692KB
commons-beanutils-1.7.0.jar 184KB
spring-tx-4.1.6.RELEASE.jar 247KB
spring-websocket-4.1.6.RELEASE.jar 379KB
spring-expression-4.1.6.RELEASE.jar 253KB
spring-context-4.1.6.RELEASE.jar 1002KB
spring-aspects-4.1.6.RELEASE.jar 55KB
ezmorph-1.0.3.jar 76KB
spring-jdbc-4.1.6.RELEASE.jar 417KB
spring-instrument-4.1.6.RELEASE.jar 7KB
spring-web-4.1.6.RELEASE.jar 702KB
commons-logging.jar 44KB
spring-test-4.1.6.RELEASE.jar 491KB
spring-orm-4.1.6.RELEASE.jar 367KB
spring-aop-4.1.6.RELEASE.jar 351KB
spring-context-support-4.1.6.RELEASE.jar 174KB
commons-logging-1.2.jar 60KB
spring-core-4.1.6.RELEASE.jar 984KB
spring-messaging-4.1.6.RELEASE.jar 279KB
commons-lang-2.5.jar 273KB
json-lib-2.4-jdk15.jar 155KB
spring-webmvc-portlet-4.1.6.RELEASE.jar 173KB
spring-jms-4.1.6.RELEASE.jar 263KB
mysql-connector-java-8.0.17.jar 2.21MB
spring-webmvc-4.1.6.RELEASE.jar 762KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 470B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 567B
org.eclipse.jdt.core.prefs 364B
src
com
tutorialspoint
StudentDAO.java 1KB
Student.java 492B
MainApp.java 1KB
StudentMapper.java 467B
StudentJDBCTemplate.java 2KB
yfw
DatabaseAccess.java 4KB
Beans.xml 951B
.project 906B
.classpath 842B
build
classes
com
tutorialspoint
MainApp.class 3KB
Student.class 960B
StudentJDBCTemplate.class 3KB
StudentMapper.class 1KB
StudentDAO.class 523B
yfw
DatabaseAccess.class 3KB
Beans.xml 951B
共 52 条
- 1
资源评论
Yfw&武
- 粉丝: 210
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功