创建数据库:
CREATE DATABASE jdbcDemo;
创建数据库表:
CREATE TABLE employee(
id INT PRIMARY KEYAUTO_INCREMENT,
name VARCHAR(20),
age INT
);
插入数据:
INSERT INTO employee VALUES(NULL,'貂蝉',18);
INSERT INTO employee VALUES(NULL,'小乔',19);
INSERT INTO employee VALUES(NULL,'大乔',20);
public class Demo {
@Test
public void query() throws Exception {
// 1.加载数据库驱动
DriverManager.registerDriver(newDriver());//导包是java.sql的包
// 2.建立数据库连接
// jdbc:mysql:连接mysql数据库的固定写法,localhost:ip地址,3306:端口号,jdbcDemo:数据库名称
String url ="jdbc:mysql://localhost:3306/jdbcDemo";
// 第一个参数:连接mysql数据库,第二和第三个参数分别是数据库的用户名和密码
Connection con =DriverManager.getConnection(url, "root", "root");
// 3.获取statement对象
Statement st =con.createStatement();
// 4.执行sql语句,并且获取结果
String sql ="select * from employee";
ResultSet res = st.executeQuery(sql);
// 5.处理结果
while (res.next()) {
System.out.println(res.getString("name"));
}
// 6.释放资源
res.close();
st.close();
con.close();
}
}
public class Demo {
@Test
public void query() throws Exception {
// 1.加载数据库驱动
//DriverManager.registerDriver(new Driver());
Class.forName("com.mysql.jdbc.Driver");
// 2.建立数据库连接
// jdbc:mysql:连接mysql数据库的固定写法,localhost:ip地址,3306:端口号,jdbcDemo:数据库名称
String url ="jdbc:mysql://localhost:3306/jdbcDemo";
// 第一个参数:连接mysql数据库,第二和第三个参数分别是数据库的用户名和密码
Connection con =DriverManager.getConnection(url, "root", "root");
// 3.获取statement对象
Statement st =con.createStatement();
// 4.执行sql语句,并且获取结果
String sql ="select * from employee";
ResultSet res =st.executeQuery(sql);
// 5.处理结果
while (res.next()) {
System.out.println(res.getString("name"));
}
// 6.释放资源
res.close();
st.close();
con.close();
}
}
@Test
public void update() throws Exception {
// 1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.建立数据库连接
// jdbc:mysql:连接mysql数据库的固定写法,localhost:ip地址,3306:端口号,jdbcDemo:数据库名称
String url ="jdbc:mysql://localhost:3306/jdbcDemo";
// 第一个参数:连接mysql数据库,第二和第三个参数分别是数据库的用户名和密码
Connection con =DriverManager.getConnection(url, "root", "root");
// 3.获取statement对象
Statement st =con.createStatement();
// 4.执行sql语句。
String sql ="update employee set age = 21";
int count =st.executeUpdate(sql);
System.out.println("您总共操作了..."+count+"...条记录");
// 5.释放资源
st.close();
con.close();
}
@Test
public void execute () throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//后去连接
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo"
,"root", "root");
//定义sql
//String sql ="select * from employee";
//更新貂蝉的年龄为18岁
String sql ="update employee set age= 18 where name = '貂蝉'";
//创建sql操作对象
Statement st =con.createStatement();
//执行查询,并且返回boolean
boolean flag = st.execute(sql);
//判断flag是否为true来判断到底是不是查询语句
if (flag) {
//如果是true,就证明是一个查询语句,需要获取结果集
ResultSet res = st.getResultSet();
while(res.next()){
System.out.println(res.getInt("id")+"..."+res.getString("name")+"..."+
res.getInt("age"));
}
res.close();
}else{
//如果是false,就证明不是查询语句
System.out.println("sql语句执行成功!一共操作了"+st.getUpdateCount()+"条sql语句");
}
// 释放资源
st.close();
con.close();
}
| boolean | next() 将光标从当前位置向前移一行。 |
boolean | absolute(int row) 将光标移动到此 ResultSet 对象的给定行编号。 |
void | afterLast() 将光标移动到此 ResultSet 对象的末尾,正好位于最后一行之后。 |
void | beforeFirst() 将光标移动到此 ResultSet 对象的开头,正好位于第一行之前。 |
boolean | previous() 将光标移动到此 ResultSet 对象的上一行。 |
public class Employee {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee[id=" + id + ", name=" + name + ", age=" + age +"]";
}
}//需求:从数据库中查询所有数据,并将每一行记录封装到一个Employee对象中,并且遍历所有数据库中查询出来的人员信息。
@Test
public void resultSet() throws Exception{
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","root");
//定义sql
String sql ="select * from employee";
//创建sql的操作对象
Statement st =con.createStatement();
//发送sql,获取结果集
ResultSet res =st.executeQuery(sql);
//创建一个集合用来存储对象
List<Employee>list = new ArrayList<Employee>();
//处理结果集
while(res.next()){
//将获取的数据封装的对象中
Employee emp = new Employee();
emp.setId(res.getInt("id"));
emp.setName(res.getString("name"));
emp.setAge(res.getInt("age"));
list.add(emp);
}
System.out.println(list);
//absolute:获取指定行的数据,数据库行数索引号的起始值从0开始
boolean absolute = res.absolute(1);
if (res.next()) {
System.out.println(res.getInt("id")+"..."+res.getString("name")+res.getInt("age"));
}
//afterLast:将光标指向最后一行记录之下。
res.afterLast();
//previous:获取当前光标往上一行的数据
res.previous();
System.out.println(res.getInt("id")+"..."+res.getString("name")+res.getInt("age"));
//beforeFirst:将光标执行第一行记录之上。
res.beforeFirst();
if (res.next()) {
System.out.println(res.getInt("id")+"..."+res.getString("name")+res.getInt("age"));
}
res.close();
st.close();
con.close();
}
@Test
public void close() {
Connection con = null;
Statement st = null;
ResultSet res = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
con =DriverManager.getConnection("jdbc:mysql:///jdbcDemo","root", "root");
st = con.createStatement();
// 执行查询
res = st.executeQuery("select count(*) as num fromemployee");
// 遍历数据
if (res.next()) {
System.out.println(res.getInt("num"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (res != null) {
res.close();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void insert() {
Connection con = null;
Statement st = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
con =DriverManager.getConnection("jdbc:mysql:///jdbcDemo","root", "root");
// 创建数据库操作对象
st = con.createStatement();
// 执行插入操作
st.executeUpdate("insert into employee values(null,'洛神',21)");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void update(){
Connection con = null;
Statement st = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
con =DriverManager.getConnection("jdbc:mysql:///jdbcDemo","root", "root");
// 创建数据库操作对象
st = con.createStatement();
// 执行插入操作
int count = st.executeUpdate("update employee set age = 22where id = 4");
System.out.println("一共修改了"+count+"条记录");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void delete() {
Connection con = null;
Statement st = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
con =DriverManager.getConnection("jdbc:mysql:///jdbcDemo","root", "root");
// 创建数据库操作对象
st = con.createStatement();
// 执行插入操作
int count = st.executeUpdate("delete from employee where id= 4");
System.out.println("您成功删除了" + count + "条记录");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void select() {
Connection con = null;
Statement st = null;
ResultSet res = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
con =DriverManager.getConnection("jdbc:mysql:///jdbcDemo","root","root");
// 创建数据库操作对象
st = con.createStatement();
// 执行插入操作
res = st.executeQuery("select * from employee where idbetween 1 and 4");
// 定义集合,将数据存入到集合中
List<Employee> list = new ArrayList<Employee>();
// 遍历封装
while (res.next()) {
System.out.println(res.getInt("id")+ "..." + res.getString("name") + "..." +res.getInt("age"));
// 封装到对象中
Employee employee = newEmployee();
employee.setId(res.getInt("id"));
employee.setName(res.getString("name"));
employee.setAge(res.getInt("age"));
// 将数据存入集合
list.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (res != null) {
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1. public class JdbcUtils {
2. // 数据库驱动只需要加载一次
3. // static静态代码块在类加载的时候会执行,并且只执行一次
4. static {
5. try {
6. Class.forName("com.mysql.jdbc.Driver");
7. } catch(ClassNotFoundException e) {
8. e.printStackTrace();
9. }
10. }
11.
12. // 对外提供获取连接的方法
13. // 异常可以直接抛出,因为在调用该方法处还会执行try-catch.
14. public static Connection getConnection() throws SQLException {
15. String url ="jdbc:mysql:///jdbcDemo";
16. String user ="root";
17. String password ="root";
18. Connection con =DriverManager.getConnection(url, user, password);
19. return con;
20. }
21.
22. //释放资源方法2
23. //其他增删改释放资源
24. public static void release(Connection con, Statement st) {
25. try {
26. if (st != null) {
27. st.close();
28. }
29. } catch (SQLException e){
30. e.printStackTrace();
31. }
32. try {
33. if (con != null) {
34. con.close();
35. }
36. } catch (SQLException e){
37. e.printStackTrace();
38. }
39. }
40. //释放资源方法1
41. // 查询的时候释放资源
42. public static void release(Connection con, Statement st,ResultSet res) {
43. try {
44. if (res != null) {
45. res.close();
46. }
47. } catch (SQLException e){
48. e.printStackTrace();
49. }
50. try {
51. if (st != null) {
52. st.close();
53. }
54. } catch (SQLException e){
55. e.printStackTrace();
56. }
57. try {
58. if (con != null) {
59. con.close();
60. }
61. } catch (SQLException e){
62. e.printStackTrace();
63. }
64. }1. @Test
2. public void select2() {
3. Connection con = null;
4. Statement st = null;
5. ResultSet res = null;
6. try {
7. con= JdbcUtil.getConnection();
8. String sql = "select * from employee";
9. st = con.createStatement();
10. res = st.executeQuery(sql);
11. List list = new ArrayList();
12. // 处理结果集
13. while (res.next()) {
14. Employee emp = newEmployee();
15. emp.setId(res.getInt(1));
16. emp.setName(res.getString(2));
17. ;
18. emp.setAge(res.getInt(3));
19. // 将对象存入集合
20. list.add(emp);
21. }
22. System.out.println(list);
23. } catch (Exception e) {
24. e.printStackTrace();
25. } finally {
26. JdbcUtil.release(con, st, res);
27. }
28. }
1. public class JdbcUtils {
2. static String url;
3. static String user;
4. static String password;
5. static String driverClass;
6. // 数据库驱动只需要加载一次
7. // static静态代码块在类加载的时候会执行,并且只执行一次
8. static {
9. try {
10. Properties pro = new Properties();
11. InputStream inputStream = newFileInputStream("src/jdbc.properties");
12. //加载properties文件
13. pro.load(inputStream);
14. //读取properties文件中的数据
15. driverClass = pro.getProperty("driverClass");
16. url=pro.getProperty("url");
17. user=pro.getProperty("user");
18. password=pro.getProperty("password");
19. Class.forName(driverClass);
20. } catch (Exception e) {
21. e.printStackTrace();
22. }
23. }
24.
25. // 对外提供获取连接的方法
26. // 异常可以直接抛出,因为在调用该方法处还会执行try-catch.
27. public static Connection getConnection() throws SQLException {
28. Connection con =DriverManager.getConnection(url, user, password);
29. return con;
30. }
31.
32. //其他增删改释放资源
33. public static void release (Connection con, Statement st){
34. try {
35. if (con != null) {
36. con.close();
37. }
38. } catch (SQLException e){
39. e.printStackTrace();
40. }
41. try {
42. if (st!=null) {
43. st.close();
44. }
45. } catch (SQLException e){
46. e.printStackTrace();
47. }
48. }
49.
50. // 查询的时候释放资源
51. public static void release(Connection con, Statement st,ResultSet res) {
52. try {
53. if (con != null) {
54. con.close();
55. }
56. } catch (SQLException e){
57. e.printStackTrace();
58. }
59. try {
60. if (st!=null) {
61. st.close();
62. }
63. } catch (SQLException e){
64. e.printStackTrace();
65. }
66. try {
67. if (res!=null) {
68. res.close();
69. }
70. } catch (SQLException e){
71. e.printStackTrace();
72. }
73. }
74. }| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |