首页 > 其他分享 >10.30

10.30

时间:2023-10-30 23:11:54浏览次数:34  
标签:return String rs 10.30 state public name

今天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

相关文章

  • 10.30 献花
    我要学习crimson000的魔怔精神妈的啥玩意啊,去年同一道题写暴力写了70,今年就只有50了??去年能切的题今年挂20,合着往死里退步是吧......
  • 每日总结10.30
    今天上课完成了两个软件设计的实验,下午做了java程序设计的期中测试取得了满分,然后做了一些软考题。  ......
  • 10.30日
    今天早上是机械拆装实训,我们以自行车为例展开了课程教学,以五人小组为单位展开了动手实践。下午极限三小时小考了一下,王老师的简单期中测试罢了(才不是呢)。虽然我周末进行了提前练习,考试勉强通过,但是在三小时里还是出现了未预料到的困难——内部服务器报错500,具体原因是tag的jar包出......
  • 10.30日记
    当一个进程在等待永远不可能发生的事件时,就会产生死锁,若系统中多个进程出于死锁状态,就会造成系统死锁。 死锁产生的必要条件:   资源互斥   每个进程占有资源并等待其他资源   系统不能剥夺进程资源   进程资源图是一个环路 死锁产生后,解决措施是打破四大条件,有......
  • 2023.10.30日报
    今天继续在做ERP,昨天确实是做的有些烦躁了,因为无论如何调整都实现不了想要的功能今天冷静下来认真修改代码不得不说写代码还是很有成就感的事情 感觉就功能来说,这个页面已经比较成熟了也做了详细内容的展示和筛选排序和分页,并且后端也已经编写好了,之后的增删改查的模板基本都......
  • 10.30算法
    无重复字符的最长子串给定一个字符串s,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1:输入:s="abcabcbb"输出:3解释:因为无重复字符的最长子串是"abc",所以其长度为3。示例2:输入:s="bbbbb"输出:1解释:因为无重复字符的最长子串是"b",所以其长度......
  • 今日总结10.30
    本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解建造者模式的动机,掌握该模式的结构;2、能够利用建造者模式解决实际问题。 [实验任务一]:计算机组装使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机......
  • JAVA-10.30
    public/private#关键字public被称之为访问修饰符。public定义的方法表示任何类的任何方法都可以调用,public定义的字段语序程序中任何方法对其进行修改和读取。但是使用public字段会破坏封装,我们会经常需要让类的属性只能被所在的类自身来调用(比如学生类的个人信息,一般只......
  • 7.第三章第3节: 2021.10.30 异常处理与日志
                     ......
  • mdg 例训笔记 10.30
    cuda安装卷积神经网络卷积神经网络的作用1.降低计算量2.提取周围特征用一个特征值代表某个东西3.升降维(1*1卷积核)一个卷积核能确定一个特征图降维:......