今天Java考试部分代码如下
package bean;
public class Bean {
private int id;
private String name;
private String gaishu;
private String fangshi;
private String starttime;
private String fianltime;
private String gongyi;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setGaishu(String gaishu) {
this.gaishu = gaishu;
}
public void setFangshi(String fangshi) {
this.fangshi = fangshi;
}
public void setStarttime(String starttime) {
this.starttime = starttime;
}
public void setFianltime(String fianltime) {
this.fianltime = fianltime;
}
public void setGongyi(String gongyi) {
this.gongyi = gongyi;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGaishu() {
return gaishu;
}
public String getFangshi() {
return fangshi;
}
public String getStarttime() {
return starttime;
}
public String getFianltime() {
return fianltime;
}
public String getGongyi() {
return gongyi;
}
public Bean(int id, String name, String gaishu, String fangshi, String starttime, String fianltime, String gongyi) {
this.id = id;
this.name = name;
this.gaishu = gaishu;
this.fangshi = fangshi;
this.starttime = starttime;
this.fianltime = fianltime;
this.gongyi = gongyi;
}
@Override
public String toString() {
return "Bean{" +
"id=" + id +
", name='" + name + '\'' +
", gaishu='" + gaishu + '\'' +
", fangshi='" + fangshi + '\'' +
", starttime='" + starttime + '\'' +
", fianltime='" + fianltime + '\'' +
", gongyi='" + gongyi + '\'' +
'}';
}
}
package dao;
import Util.DBUtil;
import bean.Bean;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Dao {
public boolean add(Bean ten) throws ClassNotFoundException , SQLException
{
String sql="insert into test(name,gaishu,fangshi,starttime,finaltime,gongyi)values"
+ "('"ten.getName() + "','" + ten.getGaishu() + "','"+ ten.getStarttime() + "','" + ten.getFianltime() + "','" + ten.getFangshi() + "','" + ten.getGongyi() + "')";
Connection conn= DBUtil.getConnection();
Statement state=null;
boolean f=false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if(a>0)
f=true;
return f;
}
public Bean getbyname(String name) throws ClassNotFoundException ,SQLException
{
String sql = "select * from test where name ='" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
Bean ten = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
int id=rs.getInt("id");
String name2 = rs.getString("name");
String gaishu2 = rs.getString("gaishu");
String fangshi2 = rs.getString("fangshi");
String starttime2=rs.getString("starttime");
String finaltime2 = rs.getString("finaltime");
String gongyi2 =rs.getString("gongyi");
ten = new Bean(id,name2,gaishu2,fangshi2,starttime2,fangshi2,gongyi2);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return ten;
}
//delete
public boolean delete(String name) throws SQLException, ClassNotFoundException {
String sql="delete from test where name='" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
int a = 0;
boolean f = false;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
//update
public boolean update(Bean b)
{
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=DBUtil.getConnection();
String sql="update shetuan set name=?,gaishu=?,fangshi=?,starttime=?,finaltime=?,gongyi=? where id=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, b.getName());
pstmt.setString(2, b.getFangshi());
pstmt.setString(3, b.getGaishu());
pstmt.setString(4, b.getStarttime());
pstmt.setString(5, b.getFianltime());
pstmt.setString(6, b.getGongyi());
pstmt.setInt(7, b.getId());
pstmt.executeUpdate();
return true;
}
catch (SQLException | ClassNotFoundException e) {
System.out.println("更新失败");
e.printStackTrace();
}
finally {
DBUtil.close(rs, pstmt, con);
}
return false;
}
//查找
public boolean name(String name) throws SQLException, ClassNotFoundException {
boolean flag = false;
String sql = "select name from test where name = '" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
public List<Bean> search(String name, String gaishu, String fangshi, String starttime) throws SQLException, ClassNotFoundException {
String sql = "select * from test where ";
if (name != "") {
sql += "name like '%" +name+ "%'";
}
if (gaishu != "") {
sql += "gaishu like '%" +gaishu+ "%'";
}
if(fangshi!="") {
sql+="fangshi like '%"+fangshi+ "%'";
}
if(starttime!="") {
sql+="starttime like '%" +starttime+ "%'";
}
List<Bean> list = new ArrayList<>();
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
Bean bean = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
int id =rs.getInt("id");
String name2 = rs.getString("name");
String gaishu2 = rs.getString("gaishu");
String fangshi2 = rs.getString("fangshi");
String starttime2=rs.getString("starttime");
String finaltime2 = rs.getString("finaltime");
String gongyi2 =rs.getString("gongyi");
bean = new Bean(id,name2,gaishu2,fangshi2,starttime2,fangshi2,gongyi2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
public List<Bean> list() throws SQLException, ClassNotFoundException {
String sql = "select * from test";
List<Bean> list = new ArrayList<>();
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
Bean bean = null;
int id=rs.getInt("id");
String name2 = rs.getString("name");
String gaishu2 = rs.getString("gaishu");
String fangshi2 = rs.getString("fangshi");
String starttime2=rs.getString("starttime");
String finaltime2 = rs.getString("finaltime");
String gongyi2 =rs.getString("gongyi");
bean = new Bean(id,name2,gaishu2,fangshi2,starttime2,fangshi2,gongyi2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
package service;
import bean.Bean;
import dao.Dao;
import java.sql.SQLException;
import java.util.List;
public class Service {
Dao tDao=new Dao();
public boolean add(Bean ten) throws SQLException, ClassNotFoundException {
boolean f = false;
if(!tDao.name(ten.getName()))
{
tDao.add(ten);
f=true;
}
return f;
}
public boolean del(String name) throws SQLException, ClassNotFoundException {
tDao.delete(name);
return true;
}
public boolean update(Bean ten)
{
tDao.update(ten);
return true;
}
public Bean getbyname(String name) throws SQLException, ClassNotFoundException {
return tDao.getbyname(name);
}
public List<Bean> search(String name, String gaishu, String fangshi, String starttime) throws SQLException, ClassNotFoundException {
return tDao.search(name,gaishu,fangshi,starttime);
}
public List<Bean> list() throws SQLException, ClassNotFoundException {
return tDao.list();
}
}
标签:return,String,rs,10.30,state,public,name From: https://www.cnblogs.com/wangzelin/p/17799152.html
}