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;
}
}
冷魇