老师这代码在运行的时候没问题,在修改一些的地方保存的时候,就抛了异常。说是跟内存泄漏的。

package course;import java.sql.Connection;import java.sql.PreparedStatement;import JDBCUtile.JDBCDemo;import test.JDBCUtie1;public class AddCourse {	public static boolean addCourse(String name,String category,String desp) {			Connection conn=null;		PreparedStatement pstet=null;		boolean flae = 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;

}


}


三金好同学
浏览 617回答 1
1回答

冷魇

暂时没看出代码的错误,请贴一下JdbcUtil1的实现以及报错,如果是第一次访问正常,第二次失败,则可能是JdbcUtil1中错误的释放了数据库连接导致的
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
MySQL