##JDBC:
1.概念 :Java Database Connectivity java 数据库连接, java语言操作数据库
*JDBC本质:其实是官方定义的一套操作所有关系型数据库的规则,即接口。
2.快熟入门:
步骤:1.导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下;2.右键-->add as library
2.注册驱动
3.获取数据库连接对象 Connection
4.定义sql
5.获取执行sql语句的对象 Statement
6.执行sql,接受返回结果
7.处理结果
8.释放资源
*代码实现 示例:
//1.导入驱动jar包
//2.注册驱动:Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象:Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db3","root",''root');
//4.定义sql: String sql="update account set balance=500 where id=1";
//5.获取执行sql语句的对象 Statement ;Statement stmt=conn.createStatement();
// 6.执行sql,接受返回结果; int count= stmt.executeUpdate(sql);
// 7.处理结果;System.out.println(count);
// 8.释放资源 ;stmt.close();conn.close();
3.详解各个对象:
1.DriverManager:驱动管理对象
*功能:1.注册驱动:告诉程序该使用哪一个数据库驱动jar
static void registerDriver(Driver driver):注册与给定的驱动程序 DriverManager.
写代码使用:Class.forName("com.mysql.jdbc.Driver");
通过查看源代码:在com.mysql.jdbc.Driver类中存在静态代码块
注意:mysql5之后的驱动jar包之后可以省略注册驱动的步骤。
2.获取数据库连接:方法:static Connection getConnection(String url,String user,String passoword) ;
参数:url:指定连接的路径;语法:jdbc:mysql://ip地址(域名):端口号/数据库名称-->举例:jdbc:mysql://locahost:3306/db3
user:用户名;
password:密码;
2.Connection:数据库连接对象
*功能:1.获取执行sql的对象
--Statement createStatement()
--PreparedStament prepareStament(String sql)
2.管理事务:
--开启事务:void setAutoCommit(boolean autoCommit):调用该方法设置参数false,即开启事务
--提交事务:commit()
--回滚事务:rollback()
3.Statement:执行sql的对象;
--boolean execute(String sql):可以执行任意的sql了解
--int executeUpdate(String sql):执行DML(insert,update,delete)语句,DDL(create,alter,drop)语句
返回值:影响的行数,可以通过这个影响的行数判断DML语句是否执行成功,返回值>0则执行成功,反之,则失败
练习:
1.account表 :添加一条记录
/*1.account表 :添加一条记录*/
public class JDBCDemo02 {
public static void main(String[] args) {
//定义全局变量
Connection conn =null;
Statement stmt =null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.定义sql
String sql="insert into account values(null,'王五',3000)";
//3.获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql://locahost:3306/db3", "root", "root");
//4.获取执行sql的Statement对象
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql);
// 6.处理结果
System.out.println(count);
if (count>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
//7.
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//避免空指针异常
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2.account表:修改记录
/*3.acounnt表,修改一条记录*/
public class JDBCDemo3 {
public static void main(String[] args) {
//便于释放资源 设置以下全局变量
Connection conn =null;
Statement stmt =null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象Connection
conn = DriverManager.getConnection("jdbc:mysql:/localhost://db3", "root", "root");
//3.定义sql
String sql="update account set balance=1500 where id=3";
//4.获取执行sql对象Statement
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql);
//6.处理结果
System.out.println(count);
if (count>0) {
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//7.释放资源
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3.account表 删除一条记录
/*
* account表:删除一条表记录
*
*/
public class JDBCDemo4 {
public static void main(String[] args) {
Connection conn =null;
Statement stmt =null;
try {
//1.注册驱动
Class.forName("com.mysql.jabc.Driver");
//2.获取数据库连接对象Connection
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
//3.定义sql语句
String sql="delete from account where id=3";
//4.获取执行sql语句的Statement对象
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql);
//6.处理结果
System.out.println(count);
if (count>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
4.Resultment:结果集对象
--next():游标向下移动一行
--getXxx(参数):获取数据
* Xxx:代表数据类型 如:int getInt(),String getString()
*参数:1.int:代表列的编号,从1开始 如:getString(1)
2.String:代表列的名称。如:getDouble("balance");
*注意:
*使用步骤:
1.游标向下移动一行
2. 判断是否有依据
3.获取数据
//循环判断游标是否是最后一行末尾
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);
}
举例练习:查询account表:
/*
* 查询account表
* */
public class JDBCDemo5 {
public static void main(String[] args) {
Connection conn =null;
Statement stmt =null;
ResultSet rs =null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接数据库对象Connection
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
//3.定义sql
String sql="select * from account";
//4.获取执行sql对象的Statement
stmt = conn.createStatement();
//5.执行sql
rs = stmt.executeQuery(sql);
//6.处理结果
//6.1 让游标向下移动一行
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 e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
5.PreparedStatement:执行sql的对象