当前位置: 首页 > >

Json加入到数据库的过程

发布时间:

获得json数据

[{"rate":"7.5","cover_x":2500,"title":"数码宝贝:最后的进化","url":"https://movie.douban.com/subject/30482645/","playable":false,"cover":"https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2623955494.webp","id":"30482645","cover_y":3667,"is_new":false},{"rate":"7.6","cover_x":2000,"title":"八佰","url":"https://movie.douban.com/subject/26754233/","playable":true,"cover":"https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615992304.webp","id":"26754233","cover_y":2800,"is_new":false}]

在java后台接收到json数据,转化为java对象

tomcat部署fastjson和mysql驱动


创建一个jdbc工具类

package jdbc;

import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class jdbc {
public Connection getConection() throws Exception{
//这是我自己的mysq设置
String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "ROOT";
String driverName = "com.mysql.jdbc.Driver";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = (Connection) DriverManager.getConnection(url, user, password);
return con;
}
public void close(Connection con,PreparedStatement ps){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void update(String sql,Object ... arg) throws Exception{
Connection con = this.getConection();
PreparedStatement ps = con.prepareStatement(sql);
for(int i=0;i
ps.setString(i+1, (String) arg[i]);
}
ps.executeUpdate();
this.close(con, ps);
}


public void listAll(HttpServletResponse response,String sql,Object ... arg) throws Exception{
PrintWriter out = response.getWriter();
Connection con = this.getConection();
PreparedStatement ps = con.prepareStatement(sql);
for(int i=0;i
ps.setString(i+1, (String) arg[i]);
}
JSONArray jsonarray = new JSONArray();
ResultSet result = ps.executeQuery();
while (result.next()) {
JSONObject jsonobj = new JSONObject();
System.out.print(result.getInt(1)+result.getString(2)+result.getString(3)+result.getString(4)+result.getString(5));
jsonobj.put("id", result.getInt(1));
jsonobj.put("name", result.getString(2));
jsonobj.put("password", result.getString(3));
jsonobj.put("sex", result.getString(4));
jsonobj.put("bir", result.getString(5));
System.out.println();
jsonarray.add(jsonobj);
}
out.print(jsonarray);
ps.close();
con.close();
}



}


创建一个servlet

package webservlet;

import java.io.IOException;
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 com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.CycleRecoverable.Context;

import java.util.*;
import JesonTest.MovieBean;
import jdbc.jdbc;



/**
* Servlet implementation class servlet
*/
@WebServlet("/register/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public servlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("text/json; charset=utf-8");
String movie = request.getParameter("movie");
System.out.println(movie);
List list = (List) JSON.parseArray(movie, MovieBean.class);
System.out.println(list);
jdbc jd = new jdbc();
String sql = "insert into movie(id,rate,title,url,cover) values(?,?,?,?,?)";
for(int i = 1; i < list.size(); i++){
try {
jd.update(sql,String.valueOf(list.get(i).id),String.valueOf(list.get(i).rate),list.get(i).title,list.get(i).url,list.get(i).cover);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}






}

}


创建一个javabean类

package JesonTest;

import java.io.Serializable;

public class MovieBean implements Serializable {
//{"rate":"7.5","cover_x":2500,"title":"数码宝贝:最后的进化",
//"url":"https://movie.douban.com/subject/30482645/","playable":false,
//"cover":"https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2623955494.webp","id":"30482645","cover_y":3667,"is_new":false}
public double rate;

public int cover_x;

public String title;

public String url;

public boolean playable;

public String cover;

public int id;

public int cover_y;

public boolean is_new;


public MovieBean() {
super();
}

public MovieBean(double rate, int cover_x, String title, String url, boolean playable, String cover, int id,
int cover_y, boolean is_new) {
super();
this.rate = rate;
this.cover_x = cover_x;
this.title = title;
this.url = url;
this.playable = playable;
this.cover = cover;
this.id = id;
this.cover_y = cover_y;
this.is_new = is_new;
}

public double getRate() {
return rate;
}

public void setRate(double rate) {
this.rate = rate;
}

public int getCover_x() {
return cover_x;
}

public void setCover_x(int cover_x) {
this.cover_x = cover_x;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public boolean isPlayable() {
return playable;
}

public void setPlayable(boolean playable) {
this.playable = playable;
}

public String getCover() {
return cover;
}

public void setCover(String cover) {
this.cover = cover;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getCover_y() {
return cover_y;
}

public void setCover_y(int cover_y) {
this.cover_y = cover_y;
}

public boolean isIs_new() {
return is_new;
}

public void setIs_new(boolean is_new) {
this.is_new = is_new;
}

@Override
public String toString() {
return "MovieBean [rate=" + rate + ", cover_x=" + cover_x + ", title=" + title + ", url=" + url + ", playable="
+ playable + ", cover=" + cover + ", id=" + id + ", cover_y=" + cover_y + ", is_new=" + is_new + "]";
}

}

mysq建表


html网页请求发送数据




运行之后mysql效果如下


总结以下流程

首先把json数据放到网页上的js代码上,然后通过ajax请求,携带数据到servlet上,此时java中的servlet就接收到了数据,接受完数据然后使用json的方法把接受的数据转换为对象,然后调用jdbc中插入数据的方法,把数据插入到数据库



友情链接: