ITPub博客

首页 > 应用开发 > > 代码在运行的时候没问题,运行出错

代码在运行的时候没问题,运行出错

作者:doudourabbit 时间:2019-09-19 16:56:07 0 删除 编辑

代码在运行的时候没问题,在修改一些的地方保存的时候,就抛了异常。说是跟内存泄漏的。
packagecourse;importjava.sql.Connection;importjava.sql.PreparedStatement;importJDBCUtile.JDBCDemo;importtest.JDBCUtie1;publicclassAddCourse { publicstaticbooleanaddCourse(String name,String category,String desp) {           Connection conn=null;      PreparedStatement pstet=null; booleanflae = false; //ResultSet rs=null;                       try {      //获取连接      conn = JDBCUtie1.getConnection();     //b 编写sql      String sql = "insert into test3 values(null,?,?,?,now())";        //预编译       pstet = conn.prepareStatement(sql);       //添加sql值        pstet.setString(1,name);        pstet.setString(2,category);        pstet.setString(3,desp);                int t = pstet.executeUpdate();               if(t>0) {           System.out.println("添加成功");         flae=true;      }else {            System.out.println("添加失败");         flae=false;     }       }catch(Exception e) {         e.printStackTrace();        }finally {         JDBCUtie1.release(pstet, conn);        }               return flae;   }}

package course;

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import JDBCUtile.JDBCDemo;

import test.JDBCUtie1;

public class ShowCourse {

public static ArrayList<Course> showCourse(){

ArrayList<Course> courseList = new ArrayList();

Connection conn = null;

PreparedStatement pstet = null;

ResultSet rs = null;

try {

//获取连接

conn = JDBCUtie1.getConnection();

//编写sql

String sql = "select * from test3";

//预编译sql

pstet = conn.prepareStatement(sql);

//执行sql

rs = pstet.executeQuery();

while(rs.next()) {

int id = rs.getInt(1);

String name = rs.getString(2);

String category = rs.getString(3);

String desp = rs.getString(4);

Date createTime = rs.getDate(5);

//System.out.println(id+" "+name+" "+category+" "+desp+" "+createTime+" ");

//将查询的信息保存到Course

Course course = new Course(id,name,category,desp,createTime);

courseList.add(course);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

JDBCUtie1.release(rs, pstet, conn);

}

//返回Arrtylist集合

return courseList;

}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4809/viewspace-3161921/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    193
  • 访问量
    1437