数据库插入github的信息

This commit is contained in:
sjm
2023-07-08 01:50:32 +08:00
parent db1d14e740
commit 502cb9fb0c
4 changed files with 299 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.hellogithub.dao;
import com.hellogithub.entity.githubEntity;
import com.hellogithub.entity.userEntity;
import com.hellogithub.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class githubDao {
githubEntity githubentity = new githubEntity();
public int ins_Github_info (int projectId, String name, String starImgUrl, int starCount, String mainLanguage, String isActive, int subscriber, int issues, String isOrganization, String defaultBranch, int forks, String protocol, String version){
PreparedStatement preparedStatement = null;
Connection connection = null;
int rs = 0 ;
try {
connection = JdbcUtils.getConnection();
String Sql = "INSERT INTO githubinfo(id,projectId,name,starImgUrl,starCount,mainLanguage,isActive,subscriber,issues,isOrganization,defaultBranch,forks,protocol,version,isVaild) VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,0);";
preparedStatement = connection.prepareStatement(Sql);
preparedStatement.setInt(1, projectId);
preparedStatement.setString(2, name);
preparedStatement.setString(3, starImgUrl);
preparedStatement.setInt(4, starCount);
preparedStatement.setString(5, mainLanguage);
preparedStatement.setString(6, isActive);
preparedStatement.setInt(7, subscriber);
preparedStatement.setInt(8, issues);
preparedStatement.setString(9, isOrganization);
preparedStatement.setString(10, defaultBranch);
preparedStatement.setInt(11, forks);
preparedStatement.setString(12, protocol);
preparedStatement.setString(13, version);
rs = preparedStatement.executeUpdate();
return rs;
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeConnection(preparedStatement, connection);
}
return rs;
}
}

View File

@@ -0,0 +1,161 @@
package com.hellogithub.entity;
public class githubEntity {
private int id;
private int projectId;
private String name;
private String starImgUrl;
private int starCount;
private String mainLanguage;
private String isActive;
private int subscriber;
private int issues;
private String isOrganization;
private String defaultBranch;
private int forks;
private String protocol;
private String version;
private int isVaild;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStarImgUrl() {
return starImgUrl;
}
public void setStarImgUrl(String starImgUrl) {
this.starImgUrl = starImgUrl;
}
public int getStarCount() {
return starCount;
}
public void setStarCount(int starCount) {
this.starCount = starCount;
}
public String getMainLanguage() {
return mainLanguage;
}
public void setMainLanguage(String mainLanguage) {
this.mainLanguage = mainLanguage;
}
public String getIsActive() {
return isActive;
}
public void setIsActive(String isActive) {
this.isActive = isActive;
}
public int getSubscriber() {
return subscriber;
}
public void setSubscriber(int subscriber) {
this.subscriber = subscriber;
}
public int getIssues() {
return issues;
}
public void setIssues(int issues) {
this.issues = issues;
}
public String getIsOrganization() {
return isOrganization;
}
public void setIsOrganization(String isOrganization) {
this.isOrganization = isOrganization;
}
public String getDefaultBranch() {
return defaultBranch;
}
public void setDefaultBranch(String defaultBranch) {
this.defaultBranch = defaultBranch;
}
public int getForks() {
return forks;
}
public void setForks(int forks) {
this.forks = forks;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getIsVaild() {
return isVaild;
}
public void setIsVaild(int isVaild) {
this.isVaild = isVaild;
}
public githubEntity(int id, int projectId, String name, String starImgUrl, int starCount, String mainLanguage, String isActive, int subscriber, int issues, String isOrganization, String defaultBranch, int forks, String protocol, String version, int isVaild) {
this.id = id;
this.projectId = projectId;
this.name = name;
this.starImgUrl = starImgUrl;
this.starCount = starCount;
this.mainLanguage = mainLanguage;
this.isActive = isActive;
this.subscriber = subscriber;
this.issues = issues;
this.isOrganization = isOrganization;
this.defaultBranch = defaultBranch;
this.forks = forks;
this.protocol = protocol;
this.version = version;
this.isVaild = isVaild;
}
public githubEntity(){}
}

View File

@@ -0,0 +1,11 @@
package com.hellogithub.service;
import com.hellogithub.dao.githubDao;
public class githubService {
private githubDao githubDao = new githubDao();
public int ins_Github_info(int projectId, String name, String starImgUrl, int starCount, String mainLanguage, String isActive, int subscriber, int issues, String isOrganization, String defaultBranch, int forks, String protocol, String version){
return githubDao.ins_Github_info(projectId, name,starImgUrl,starCount, mainLanguage,isActive, subscriber, issues, isOrganization,defaultBranch, forks, protocol, version);
}
}

View File

@@ -0,0 +1,79 @@
package com.hellogithub.servlet.githubinfo;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.service.githubService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
@WebServlet("/insertGithubInfo")
public class ins_Github_infoServlet extends HttpServlet {
private githubService githubservice = new githubService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
String curOrigin = req.getHeader("Origin");
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
resp.setHeader("Access-Control-Allow-Credentials", "true");
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
resp.setHeader("Access-Control-Max-Age", "3600");
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
resp.setContentType("application/json;charset=UTF-8");
PrintWriter writer = resp.getWriter();
int projectid = Integer.parseInt(req.getParameter("projectId "));
String name = req.getParameter("name ");
String starImgUrl = req.getParameter("starImgUrl ");
int starCount = Integer.parseInt(req.getParameter("starCount "));
String mainLanguage = req.getParameter("mainLanguage ");
String isActive = req.getParameter("isActive ");
int subscriber = Integer.parseInt(req.getParameter("subscriber "));
int issues = Integer.parseInt(req.getParameter("issues "));
String isOrganization = req.getParameter("isOrganization ");
String defaultBranch = req.getParameter("defaultBranch ");
int forks = Integer.parseInt(req.getParameter("forks "));
String protocol = req.getParameter("protocol ");
String version = req.getParameter("version ");
int rs = githubservice.ins_Github_info(projectid,name,starImgUrl,starCount, mainLanguage, isActive, subscriber, issues,isOrganization,defaultBranch,forks,protocol,version);
if(rs == 1){
setResultOK("插入成功", writer);
}else{
setResultError("插入失败", writer);
}
writer.close();
}
public void setResult(Integer code, String msg, PrintWriter writer) {
HashMap<String, Object> result = new HashMap<>();
result.put("code", code);
result.put("msg", msg);
String jsonString = JSONObject.toJSONString(result);
writer.println(jsonString);
writer.close();
}
public void setResultError(String msg, PrintWriter writer) {
setResult(500, msg, writer);
}
public void setResultOK(String msg, PrintWriter writer) {
setResult(200, msg, writer);
}
}