依赖的jar包
数据库结构:表名student
JDBC类,增删查改操作
package cn.com.lsh;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBC {
public static void main(String[] args) {
ComboPooledDataSource pool = new ComboPooledDataSource();
while (true) {
System.out.println("输入指令进行操作:");
System.out.println("1:查询表记录 2:新增一条记录 3:修改一条记录 4:删除一条记录");
//控制台输入获取值
Scanner in = new Scanner(System.in);
int s = in.nextInt();
if (s == 1) {
getAll(pool);
} else if (s == 2) {
Scanner in1 = new Scanner(System.in);
System.out.println("输入学生姓名");
String name = in1.nextLine();
System.out.println("输入分数");
double score = in1.nextDouble();
add(name, score,pool);
} else if (s == 3) {
Scanner in1 = new Scanner(System.in);
System.out.println("输入要修改学生的姓名");
String name = in1.nextLine();
System.out.println("输入新的分数");
double score = in1.nextDouble();
update(name, score,pool);
} else if (s == 4) {
Scanner in1 = new Scanner(System.in);
System.out.println("输入要删除学生的姓名");
String name = in1.nextLine();
delete(name,pool);
}
System.out.println();
System.out.println();
}
}
/*
* 查询所有表记录
*/
public static void getAll(ComboPooledDataSource pool) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 注册数据库驱动
conn = pool.getConnection();
// sql语句
String sql = "select * from student";
// 获取传输器
ps = conn.prepareStatement(sql);
// 执行sql语句,保存到ResultSet结果集中
rs = ps.executeQuery();
while (rs.next()) {// rs中有指针,指针开始时指向数据库的表头,
// 当指针指向内容时rs.next返回true
int id = rs.getInt("id");
String name = rs.getString("name");
String pswd = rs.getString("score");
System.out.println(id + " " + name + " " + pswd);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//将数据库连接对象放回池中
closepool.repayClose(conn, ps, rs);
}
}
/*
* 新增一条表记录
*/
public static void add(String name, Double score,ComboPooledDataSource pool) {
Connection conn = null;
//PreparedStatement是Statement的子类,拥有更多的方法,可以防止sql注入攻击
PreparedStatement ps = null;
try {
// 注册数据库驱动
conn = pool.getConnection();
//sql语句的骨架
String sql = "insert into student value(null,?,?)";
//获取传输器,将sql语句的骨架发送给mysql服务器
ps = conn.prepareStatement(sql);
//设置sql语句的两个??参数,
//这样做可以将sql语句的参数看为一个整体发给服务器,避免sql注入攻击
ps.setString(1, name);
ps.setDouble(2, score);
//执行sql语句,返回影响行数
int row = ps.executeUpdate();
if (row != 0) {
System.out.println("成功添加1行记录");
} else {
System.out.println("添加失败,请重新输入!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//将数据库连接对象放回池中
closepool.repayClose(conn, ps);
}
}
public static void update(String name, Double score,ComboPooledDataSource pool) {
Connection conn = null;
PreparedStatement ps = null;
try {
//注册数据库驱动,获取数据库连接
conn = pool.getConnection();
String sql = "update student set score=? where name=?";
//获取传输器
ps = conn.prepareStatement(sql);
//设置sql语句的参数score=? 和name=?
ps.setDouble(1, score);
ps.setString(2, name);
//执行sql语句,返回执行结果
int row = ps.executeUpdate();
if (row != 0) {
System.out.println("修改记录成功,影响行数" + row);
} else {
System.out.println("修改失败,请重新输入!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//对象放回池中
closepool.repayClose(conn, ps);
}
}
public static void delete(String name,ComboPooledDataSource pool) {
Connection conn = null;
PreparedStatement ps = null;
try {
//注册数据库驱动,获取数据库连接
conn = pool.getConnection();
String sql = "delete from student where name=?";
//获取传输器
ps = conn.prepareStatement(sql);
//设置sql语句的参数name=?
ps.setString(1, name);
int row = ps.executeUpdate();
if (row != 0) {
System.out.println("删除" + row + "行记录成功");
} else {
System.out.println("删除失败,请重新输入!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//对象放回池中
closepool.repayClose(conn, ps);
}
}
}
closepool类,用于对使用后的数据库连接对象放回连接池中
package cn.com.lsh;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class closepool {
public static void repayClose(Connection conn,PreparedStatement ps,ResultSet rs) {
if(conn!=null) {
try {
//close()方法如果是对单个对象,是释放资源,对对象池,是放回池中
conn.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
conn=null;
}
}
if(ps!=null) {
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
ps=null;
}
}
if(rs!=null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
rs=null;
}
}
}
//方法重载,用于只用到两个参数的方法
public static void repayClose(Connection conn,PreparedStatement ps) {
if(conn!=null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
conn=null;
}
}
if(ps!=null) {
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
ps=null;
}
}
}
}