JDBC Helloworld
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
//import com.mysql.jdbc.Driver;
public class HelloJDBC {
@Test
public void method() {
/*
* 1.导入驱动
* 新建lib文件夹用来存放导入的包,将包Add to Build Path
* 2.注册驱动
* 3.获取连接对象
* 4.根据连接对象,获取可以执行SQL语句的对象
* 5.执行SQL语句,获取结果集
* 6.操作结果集
* 7.释放资源
*/
try {
//2.注册驱动
//使用下边的方式注册驱动会注册两次,使用的话需要导包
// DriverManager.registerDriver(new Driver());
//Driver dir = new Driver();//也可以直接new Driver();
//直接只单独加载:Dirver.class 该字节码文件,推荐使用反射的方式加载驱动,只加载一次
//如果忘记类名,可以写一个Driver之后导包,复制"com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver");
//3.获取连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123");
//4.根据连接对象,获取可以执行SQL语句的对象
Statement stat = conn.createStatement();
//5.执行SQL语句,获取结果集
String sql = "select * from student;";//后边可以不加分号
ResultSet rs = stat.executeQuery(sql);
//6.操作结果集
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + "------" + name);
}
//7.释放资源
rs.close();
stat.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如上的释放资源会有问题,通过try catch finally代码嵌套进行优化,如下代码是实现了增删改查四个功能。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
/**
* JDBC CURD
* @author me
*/
public class JDBC_CURD {
/**
* 增
*/
@Test
public void method1() {
Connection conn = null;
Statement stat = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获得数据库链接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123");
//根据链接对象,获取可以执行SQL语句的对象
stat = conn.createStatement();
//执行SQL语句
String sql = "insert into student(id, name) values(null, 'zhuba');";//一次只能执行一条SQL语句
int r = stat.executeUpdate(sql);
if(r > 0) {
System.out.println("添加成功");
System.out.println(r + "行受到影响");
} else {
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stat != null) {
stat.close();
stat = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 删
*/
@Test
public void method2() {
Connection conn = null;
Statement stat = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取和数据库的物理链接
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/testjdbc", "root", "123");
//通过数据库链接对象,获取执行SQL语句的对象
stat = conn.createStatement();
//执行SQL语句
String sql = "delete from student where name = 'zhangsan'";
int r = stat.executeUpdate(sql);
if(r > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stat != null) {
stat.close();
stat = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 改
*/
@Test
public void method3() {
Connection conn = null;
Statement stat = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获得数据库链接对象
conn = DriverManager.getConnection("jdbc:mysql:///testjdbc", "root", "123");
//通过数据库链接对象获取可以执行SQL语句的对象
stat = conn.createStatement();
//执行SQL语句
String sql = "update student set name = 'zhangsan_2' where name = 'zhangsan';";
int r = stat.executeUpdate(sql);
if(r > 0) {
System.out.println("修改成功");
System.out.println(r + "行受到影响");
} else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(stat != null) {
stat.close();
stat = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 查
*/
@Test
public void method4() {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得与数据库的链接
conn = DriverManager.getConnection("jdbc:mysql:///testjdbc", "root", "123");
// 获得执行SQL的对象
stat = conn.createStatement();
// 执行SQL语句,获得结果集
String sql = "select * from student;";
rs = stat.executeQuery(sql);
// 操作结果集
while (rs.next()) {
// 获得该行该列的对象
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + "---" + name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭链接
try {
if (rs != null) { //避免未给对象赋值,导致.close()空指针异常
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
stat = null;// GC优先回收null对象
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
Statement 的 executeQuary(),executeUpdate(),execute() 三者的区别:
- ResultSet executeQuery(String sql) throws SQLException
Executes the given SQL statement, which returns a single ResultSet object.
- int executeUpdate(String sql) throws SQLException
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
- boolean execute(String sql) throws SQLException
Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string.