使用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或者无用功的地方。希望大大门不要介意
阅读全文
1 0
- 使用Spring连接数据库,配置JSP json数据
- 使用Spring连接数据库数据创建JSON数据 作为接口
- spring配置连接数据库
- spring两种不同的配置,使用c3p0连接数据库
- Spring中使用JDBC连接数据库的配置
- Spring中使用纯JDBC连接数据库的配置
- Spring配置数据库的连接
- 配置Spring连接mysql数据库
- Spring 配置数据连接池
- spring学习笔记(17)数据库配置[1]spring数据连接池详解
- Jsp读取数据库返回json数据,Android客户端接收json
- Jsp读取数据库返回json数据,Android客户端接收json
- Jsp读取数据库返回json数据,Android客户端接收json
- jsp 连接数据库 数据表格显示
- 在Jsp中三种连接数据库的配置
- JSP连接MySQL数据库及环境配置
- 使用Spring连接mysql数据库
- 使用Spring连接MySql数据库
- 剑指offer-08:旋转数组的最小数字
- log4j 的使用详解
- PHP 和 AJAX MySQL 数据库实例
- js中Object常用方法和属性
- MySQL优化所调整的10项配置
- 使用Spring连接数据库,配置JSP json数据
- Linux shell script基础学习
- JS学习-Map和Set
- 8月8 某公司一面
- leetcode--Invert Binary Tree
- 3D控件Aspose.3D 8月新版V17.8发布 | 支持6面多维地图
- 读取win或linux绝对路径下的属性文件
- 【Linux】Linux中常用操作命令
- iOS开发