解决反复插入info问题

This commit is contained in:
sjm
2023-07-08 15:14:51 +08:00
parent a77460d28e
commit 6e8fb74eec

View File

@@ -4,6 +4,7 @@ import com.hellogithub.entity.githubEntity;
import com.hellogithub.entity.projectEntity; import com.hellogithub.entity.projectEntity;
import com.hellogithub.entity.userEntity; import com.hellogithub.entity.userEntity;
import com.hellogithub.utils.JdbcUtils; import com.hellogithub.utils.JdbcUtils;
import com.mysql.cj.jdbc.JdbcConnection;
import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.BeanListHandler;
@@ -19,36 +20,46 @@ public class githubDao {
public int ins_Github_info (int projectId, String avatar,String name, String starImgUrl, int starCount, String mainLanguage, String isActive, int subscriber, int issues, String isOrganization, String defaultBranch, int forks, String protocol, String version){ public int ins_Github_info (int projectId, String avatar,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; PreparedStatement preparedStatement = null;
Connection connection = null; Connection connection = null;
int rs = 0 ; ResultSet rs = null;
String sql1 = "SELECT * FROM githubinfo WHERE projectId = ?;";
int back = 0 ;
try { try {
connection = JdbcUtils.getConnection(); Connection conn = JdbcUtils.getConnection();
String Sql = "INSERT INTO githubinfo(id,projectId,avatar,name,starImgUrl,starCount,mainLanguage,isActive,subscriber,issues,isOrganization,defaultBranch,forks,protocol,version,isVaild) VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0);"; PreparedStatement prep = conn.prepareStatement(sql1);
preparedStatement = connection.prepareStatement(Sql); prep.setInt(1,projectId);
preparedStatement.setInt(1, projectId); rs = prep.executeQuery();
preparedStatement.setString(2, avatar); if(rs == null){
preparedStatement.setString(3, name); connection = JdbcUtils.getConnection();
preparedStatement.setString(4, starImgUrl); String Sql = "INSERT INTO githubinfo(id,projectId,avatar,name,starImgUrl,starCount,mainLanguage,isActive,subscriber,issues,isOrganization,defaultBranch,forks,protocol,version,isVaild) VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0);";
preparedStatement.setInt(5, starCount); preparedStatement = connection.prepareStatement(Sql);
preparedStatement.setString(6, mainLanguage); preparedStatement.setInt(1, projectId);
preparedStatement.setString(2, avatar);
preparedStatement.setString(3, name);
preparedStatement.setString(4, starImgUrl);
preparedStatement.setInt(5, starCount);
preparedStatement.setString(6, mainLanguage);
preparedStatement.setString(7, isActive); preparedStatement.setString(7, isActive);
preparedStatement.setInt(8, subscriber); preparedStatement.setInt(8, subscriber);
preparedStatement.setInt(9, issues); preparedStatement.setInt(9, issues);
preparedStatement.setString(10, isOrganization); preparedStatement.setString(10, isOrganization);
preparedStatement.setString(11, defaultBranch); preparedStatement.setString(11, defaultBranch);
preparedStatement.setInt(12, forks); preparedStatement.setInt(12, forks);
preparedStatement.setString(13, protocol); preparedStatement.setString(13, protocol);
preparedStatement.setString(14, version); preparedStatement.setString(14, version);
rs = preparedStatement.executeUpdate(); back = preparedStatement.executeUpdate();
return rs; }else{
return back;
}
return back;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
JdbcUtils.closeConnection(preparedStatement, connection); JdbcUtils.closeConnection(preparedStatement, connection);
} }
return rs; return back;
} }
public List<githubEntity> ret_Github_info(int projectid){ public List<githubEntity> ret_Github_info(int projectid){