1:查询数据库中所有数据
public List<MomentInfo> queryAllData() {
Connection connection = null;
PreparedStatement preparedStatement = null;
List<ChipBurningRegistration> list = new ArrayList<ChipBurningRegistration>();
ResultSet rs = null;
ArrayList<MomentInfo> list1 = new ArrayList<MomentInfo>();
try {
String sql = "SELECT * from XXX ";
connection = Oracle202.getConnection();
preparedStatement = connection.prepareStatement(sql);
rs = preparedStatement.executeQuery();
while (rs.next()) {//rs指的是result
MomentInfo momentInfo = new MomentInfo();
momentInfo.setId(rs.getInt("ID"));
momentInfo.setTeam(rs.getString("team"));
momentInfo.setAssetNo(rs.getString("assetNo"));
momentInfo.setMomentName(rs.getString("momentName"));
momentInfo.setMomentFlag(rs.getString("managerFlag"));
momentInfo.setMomentCategory(rs.getString("momentCategory"));
momentInfo.setStation(rs.getString("station"));
momentInfo.setSpecification(rs.getString("specification"));
momentInfo.setCheckCycle(String.valueOf(rs.getInt("checkCycle")));
momentInfo.setMomentRange(rs.getString("momentRange"));
list1.add(momentInfo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Oracle202.releaseDB(null, preparedStatement, connection);
}
/*System.out.println(list1.toString());*/
return list1;
}
2:删除数据库中所有数据
public Boolean deleteAllData(){//删除对应数据库中所有数据
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
boolean flag = false;
String sql = "DELETE FROM XXXX";
try {
connection = Oracle202.getConnection();//数据库的名字.getConnection();即可连接到数据库;getConnection()是在util中创建数据库连接时写的方法
preparedStatement = connection.prepareStatement(sql);//预编译
int i = preparedStatement.executeUpdate();//返回的是增删改查所影响的行数;注意该方法是无参方法
if (i > 0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Oracle202.releaseDB(rs, preparedStatement, connection);//释放资源:结果集对象,预编译对象、连接对象;releaseDB()是在util中创建数据库连接时写的释放资源的方法
}
return flag;
}
总结:
public boolean XXXX(){
//1.创建连接对象connection
Connection c=null;
//2.创建预编译对象
PreparedStatement p=null;
//3.创建结果集对象
ResultSet rs=null;
//4.创建sql语句;
String sql="select * from XX1";
//5.连接数据库
c=XX1.getConnection();
//6.预编译
try {
//将会抛出 try..catch异常;根据提示进行抛出即可;接下来都在try中进行即可
p=c.prepareStatement(sql);//预编译时调用的是connection中的prepareStatement(sql)方法;
//7.返回增删改查所影响的行数,主要用来判断sql语句是否已经执行
int i=p.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
//8.释放资源
finally {
XX1.releaseDB(rs, p, c);//释放资源:结果集对象,预编译对象、连接对象;releaseDB()是在util中创建数据库连接时写的释放资源的方法
}
return true;//因为定义的是Boolean类型的方法,所以必须有个Boolean类型的返回值,否则会报错
}