使用Spring连接数据库,配置JSP json数据

来源:互联网 发布:樱井知香怎么喷那么多 编辑:程序博客网 时间:2024/05/17 06:37

1.使用Spring连接数据库。 我使用的是Mysql

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:lang="http://www.springframework.org/schema/lang"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean name="da" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver" /><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testcsf"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean></beans>
2.创建一个连接数据库的类。

把数据库里面的数据保存到JSONObject里面。在添加到JSONArray里面 最后return回来。

public class test {/** * @param args * @throws Exception  */String sql = "";JSONObject jsonbo = new JSONObject();JSONArray jsonay=new JSONArray();public JSONArray Json() throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext("jdcb.xml");DataSource dataSource = ctx.getBean("da", DataSource.class); // 获取bean配置sql = "select * from test ";// -------------------------json 测试/* 连接 */Connection connection = dataSource.getConnection(); // 开放连接Statement stm = connection.createStatement(); // 发送sql语句ResultSet rs = stm.executeQuery(sql);while (rs.next()) {      //根据数据库的字段进行put 添加。      //jsonbo.put("name", rs.getString("name"));//name为数据库里的字段明   也可以使用下标查询 rs.getString(1)jsonbo.put("id", rs.getString("id"));jsonbo.put("name", rs.getString("name"));jsonay.add(jsonbo);//把jsonobject 添加到json里面  通过下面的实现类答应出来}return jsonay;  }}
3.创建一个实现的类'

使用doget方法 和dopost

public class jsontest extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubresp.setContentType("text/plain");  //防止JSP翻译的时候出现中文乱码        resp.setCharacterEncoding("gb2312");  PrintWriter  out=new PrintWriter(resp.getOutputStream());test t =new test();try {t.Json();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}out.print(t.jsonay.toString());out.flush();    //刷新out.close();    //关闭}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubthis.doGet(req, resp);}}
4.xml文件的配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>hellojson</display-name><welcome-file-list><welcome-file>json.jsp</welcome-file></welcome-file-list><servlet><servlet-name>JsonServlet</servlet-name><servlet-class>com.json.test.jsontest</servlet-class></servlet><servlet-mapping><servlet-name>JsonServlet</servlet-name><url-pattern>/getJson</url-pattern></servlet-mapping></web-app>  

  

5.jsp页面的编辑

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><% response.setHeader("Pragma","No-cache");    response.setHeader("Cache-Control","no-cache");    response.setDateHeader("Expires", -10);   %>  <!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><form action="getJson" method="post"><input type="submit" value="提交" /></form></body></html>

注释:本人也是在学习,写的代码可能会存在BUG或者无用功的地方。希望大大门不要介意

 
原创粉丝点击