12周
作者:互联网
这一周,建民哥给我们进行了测试, 冲刺的内容是论文爬虫系统。
package Dao;
import Bean.bean;
import DBUtil.DBUtil;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Dao {
public List<bean> searchById(String id){
List<bean> list = new ArrayList<bean>();
try {
Connection conn = DBUtil.getConn();
Statement state = null;
String sql="select * from lunwen where id='"+id+"'";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
bean lun = new bean();
lun.setId(rs.getString("id"));
lun.setKeywords(rs.getString("keywords"));
lun.setTitle(rs.getString("title"));
lun.setLink(rs.getString("link"));
System.out.println(lun.getId());
list.add(lun);
}
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e) {
System.out.println("发生错误");
e.printStackTrace();
}
return list;
}
public List<bean> search(String id,String title,String keywords){
List<bean> list = new ArrayList<bean>();
try {
Connection conn = DBUtil.getConn();
Statement state = null;
String sql = "select * from lunwen where id like '%"+id+"%' and title like '%"+title+"%' and keywords like '%"+keywords+"%'";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
bean lu = new bean();
lu.setId(rs.getString("id"));
lu.setKeywords(rs.getString("keywords"));
lu.setTitle(rs.getString("title"));
lu.setLink(rs.getString("link"));
list.add(lu);
}
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e) {
System.out.println("发生错误");
e.printStackTrace();
}
return list;
}
public boolean add(bean bean)
{
Connection conn = DBUtil.getConn();
PreparedStatement pstmt = null;
boolean f = false;
int a=0;
try {
String sql = "insert into lunwen(title,link) value(?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bean.getTitle());
pstmt.setString(2, bean.getLink());
a = pstmt.executeUpdate();
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
DBUtil.close(pstmt, conn);
}
if(a>0)
f=true;
return f;
}
public boolean delete(String id) {
boolean flag = false;
Connection conn = DBUtil.getConn();
Statement state = null;
try {
String sql = "delete from lunwen where id = '"+id+"'";
PreparedStatement pstmt = conn.prepareStatement(sql);
int i = pstmt.executeUpdate();
pstmt.close();
conn.close();
if(i>0) flag = true;
} catch (SQLException e) {
System.out.println("删除失败!");
e.printStackTrace();
}
return flag;
}
//修改车站信息
public boolean alter(bean bean,String id) {
String sql = "update title set tltle='" + bean.getTitle() + "', link='" + bean.getLink()
+ "' where id='" + id + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
System.out.println("修改成功");
a = state.executeUpdate(sql);
System.out.println(a);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
System.out.println(f);
return f;
}
public boolean xiugai(String id,String title,String link)
{
Connection conn = DBUtil.getConn();
Statement state =null;
ResultSet rs = null;
boolean flag=false;
System.out.printf(id+title+link);
try
{
String sql = "update lunwen set title='"+title+"',link ='"+link+"' where id='"+id+"'";
state = conn.createStatement();
int count = state.executeUpdate(sql);
if(count>0) {
flag=true;
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return flag;
}
}
标签:12,String,rs,sql,id,conn,pstmt 来源: https://www.cnblogs.com/lss1226/p/16375916.html