




修改
package cn.itcast.jdbc;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcDemo01 {
public static void main(String[] args) throws Exception {
//1.导入驱动jar包
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取数据库连接对象
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT","root","root");
//4.定义SQL语句
String sql="update account1 set balance=1000 where id=1 ";
//5.获取执行SQL的对象 Statement
Statement stmt=conn.createStatement();
//6.执行sql
int count=stmt.executeUpdate(sql);
//处理结果
System.out.println(count);
//8.释放资源
stmt.close();
conn.close();
}
}
添加
package cn.itcast.jdbc;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo02 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql
String sql="insert into account1 values(null,'wangwu',3000)";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
int count=stmt.executeUpdate(sql);//影响的行数
//处理结果
System.out.println(count);
if (count>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//避免空指针异常
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
修改(2)
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo03 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql
String sql="update account1 set balance=1500 where id=2";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
int count=stmt.executeUpdate(sql);//影响的行数
//处理结果
System.out.println(count);
if (count>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//避免空指针异常
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
删除
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo04 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql
String sql="delete from account1 where id=2";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
int count=stmt.executeUpdate(sql);//影响的行数
//处理结果
System.out.println(count);
if (count>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//避免空指针异常
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
创建表
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
执行DDL语句 没有返回结果
*/
public class JDBCDemo5 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql 创建表 不常用
String sql="create table student1(id int,name varchar(20)) ";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
int count=stmt.executeUpdate(sql);//影响的行数
//处理结果
System.out.println(count);//0
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//避免空指针异常
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
查询表
package cn.itcast.jdbc;
import java.sql.*;
public class JDBCDemo06 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql 创建表 不常用
String sql="select *from account1";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
rs=stmt.executeQuery(sql);//影响的行数
//处理结果
//让游标向下移动一行
rs.next();
//获取数据
int id=rs.getInt(1);
String name=rs.getString("name");
double balance=rs.getDouble(3);
System.out.println(id+"------"+name+"----------"+balance);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源(最后申请的资源要先释放)
//避免空指针异常
if (stmt!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
查询表-遍历
package cn.itcast.jdbc;
import java.sql.*;
public class JDBCDemo07 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//定义sql 创建表 不常用
String sql="select *from account1";
//获取Connnection对象
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true","root","root");
//获取执行SQL的对象Statement
stmt=conn.createStatement();
//执行sql
rs=stmt.executeQuery(sql);//影响的行数
//6.处理结果
//让游标向下移动一行
// if (rs.next()){
//
// //获取数据
// int id=rs.getInt(1);
// String name=rs.getString("name");
// double balance=rs.getDouble(3);
// System.out.println(id+"------"+name+"----------"+balance);
// }
while (rs.next()){
//循环判断游标是否是最后一行末尾
//6.2获取数据
int id=rs.getInt(1);
String name=rs.getString("name");
double balance=rs.getDouble(3);
System.out.println(id+"------"+name+"----------"+balance);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源(最后申请的资源要先释放)
//避免空指针异常
if (stmt!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
练习:封装为对象,然后装载集合,返回
package cn.itcast.main;
import java.util.Date;
public class Emp {
private int id;
private String ename;
private int job_id;
private int mgr;
private Date joindate;
private double salary;
private double bouns;
private int dept_id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getJob_id() {
return job_id;
}
public void setJob_id(int job_id) {
this.job_id = job_id;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getJoindate() {
return joindate;
}
public void setJoindate(Date joindate) {
this.joindate = joindate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bouns=" + bouns +
", dept_id=" + dept_id +
'}';
}
}
package cn.itcast.main;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/*
定义一个方法: 查询emp表的数据,封装为对象,然后装载集合,返回
*/
public class JDBCDemo08_insert {
public static void main(String[] args) {
List<Emp> list=new JDBCDemo08_insert().findAll();
System.out.println(list);
}
/*
查询所有EMP对象
*/
public List<Emp> findAll() {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
List<Emp> list=null;
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取Connnection对象
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?&useSSL=false&serverTimezone=GMT","root","root");
//3.定义sql
String sql="select *from emp";
//4.获取执行SQL的对象
stmt=conn.createStatement();
//5.执行SQL对象
rs=stmt.executeQuery(sql);
//6.b遍历结果集,封装对象,装载集合
Emp emp=null;
list=new ArrayList<Emp>();
while (rs.next()){
//获取数据
int id=rs.getInt("id");
String ename=rs.getString("ename");
int job_id=rs.getInt("job_id");
int mgr=rs.getInt("mgr");
Date joindate=rs.getDate("joindate");
double salary=rs.getDouble("salary");
double bouns=rs.getDouble("bouns");
int dept_id=rs.getInt("dept_id");
//创建emp对象,并赋值
emp=new Emp();
emp.setId(id);
emp.setEname(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary );
emp.setBouns(bouns);
emp.setDept_id(dept_id);
//装载集合
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
}


package cn.itcast.main;
import cn.itcast.utils.JDBCUtils;
import java.sql.*;
import java.util.Scanner;
public class JDBCDemo10 {
public static void main(String[] args) {
//1.键盘录入,接受用户名和密码
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
//2.调用方法
boolean flag=new JDBCDemo10().login(username,password);
//3.判断结果,输出不同语句
if (flag){
System.out.println("登录成功");
}else{
System.out.println("用户名或密码错误");
}
}
/*
登录方法,使用PreparedStatement实现
*/
public boolean login(String username,String password){
if (username==null||password==null){
return false;
}
//连接数据库判断是否登录成功
//1.获取连接
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn= JDBCUtils.getConnection();
//2.定义sql 拼接字符串
String sql="select *from user where username= ? and password=?";
//3.获取执行的sql
pstmt=conn.prepareStatement(sql);
//给?赋值
pstmt.setString(1,username);
pstmt.setString(2,password);
//执行查询,不需要传递sql
rs=pstmt.executeQuery();
return rs.next();//如果有下一行,返回true
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
事务

package cn.itcast.main;
import cn.itcast.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 事务
*
*/
public class JDBCDemo11 {
public static void main(String []args){
//1.获取连接
Connection conn=null;
PreparedStatement pstmt1=null;
PreparedStatement pstmt2=null;
try {
conn= JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//2定义sql
//2.1张三-500
String sql1="update account1 set balance =balance-? where id=? ";
//2.2李四+500
String sql2="update account1 set balance=balance+? where id=?";
//获取执行SQL对象
pstmt1=conn.prepareStatement(sql1);
pstmt2=conn.prepareStatement(sql2);
//设置参数
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);//第一个参数
pstmt2.setInt(2,2);//第2个参数
//5.执行sql
pstmt1.executeUpdate();
//手动制造异常
// int i=3/0;
pstmt2.executeUpdate();
//提交事务
conn.commit();
} catch (Exception e) {
//事务回滚
try {
if (conn!=null){
//回滚是有异常发生的;
conn.rollback();
}
} catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.close(pstmt1,conn);
// JDBCUtils.close(pstmt2,null);
}
}
}
- 注意在哪个位置会发生异常,然后开启事务;
- 在cathch中进行使用rollback()方法进行回滚;
数据库连接池
- 概念:其实就是一个容器(集合),当存数据库连接的容器
- 当系统初始化好好,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。
*好处
-1.节约资源
-2.用户访问高效
实现
1.标准接口: DateSource javax.sql包下
1.方法:
-获取连接:getConnection()
-归还连接:如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接;
2.一般我们不去实现它,有数据库厂商来实现
1.c3p0:数据库连接池技术;
2.Druid:数据库连接池实现技术,由阿里巴巴提供的。
本文详细介绍了Java JDBC进行数据库操作的内容,包括修改、添加、删除、创建表、查询表、遍历查询结果、事务处理以及数据库连接池的原理和使用,特别提到了Spring JDBC中的JdbcTemplate作为更高级的JDBC抽象。
2487

被折叠的 条评论
为什么被折叠?



