JDBC:概念 Java Database Connectivity Java数据库连接
本质 官方定义了操作所有关系型数据库的规则(接口),各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套(JDBC)接口编程,真正执行的代码是驱动jar包中的实现类。
下载地址:http://mvnrepository.com/artifact/mysql/mysql-connector-java
快速入门:
*步骤:1.导入驱动jar包 mysql-connector-java-5.1.37.jar
1.复制包到目录下
2.右键选择Add as Library
//1.导入驱动jar包
//2.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
//3.获取数据库的连接对象 connnection
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/bktest”,“root”,“11111”);
//4.定义sql
String sql = “update info set age = 20 where name = ‘张三’”;
//5.获取执行sql语句的对象 statement
Statement stmt = conn.createStatement();
//6.执行sql,接收返回的结果
int cout = stmt.executeUpdate(sql);
//7.处理结果
System.out.println(cout);
//8.释放资源
stmt.close();
conn.close();
3.详解各个对象
1.DriverManager:驱动管理对象
*功能:注册驱动
获取数据库连接
查看源码发现存在静态代码块
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException(“Can’t register driver!”);
}
2.获取数据库连接
方法:static connection getConnection(string url, string user, string password)
url:连接的路径
语法:jdbc:mysql://IP(域名):端口/数据库名
jdbc:mysql://localhost:3306/bktest
2.Connnection:连接对象
1功能
1.获取执行sql的对象
*statement getStatement
*preparedStatement PreparedStatement(string sql)
2.管理事务
*开启事务 void setAutoCommit(boolean autoCommit)调用该方法设置false,即开启事务
*提交事务 commit()
*回滚事务 rollback()
3.Statement:执行SQL的对象
1.执行sql
boolean execute(String sql) 可以执行任意的sql (了解)
int executeUpdate(String sql) 执行给定的SQL语句,这可能是INSERT , UPDATE ,或DELETE语句,或者不返回任何内容,如SQL DDL语句的SQL语句。
ResultSet executeQuery(String sql) 执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。通常为静态 SQL SELECT 语句
2.练习
*添加一条记录
Connection conn = null;
Statement st = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//定义sql
String sql = "insert into info(name,age,dep_name) values('小天',43,'研发部')";
//获取connection对象
String url = "jdbc:mysql://127.0.0.1:3306/bktest";
String user = "root";
String password = "11111";
conn = DriverManager.getConnection(url,user,password);
//获取sql对象 statement
st = conn.createStatement();
//执行sql语句
int cout = st.executeUpdate(sql);
System.out.println(cout);
if (cout>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
if (st != null){
try {
st.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (conn != null){
conn.close();
}
}
*修改一条记录
public static void main(String[] args) {
Connection conn = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "update student set age = '50' where id =8";
conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "111111");
statement = conn.createStatement();
int i = statement.executeUpdate(sql);
if (i>0){
System.out.println("修改成功");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (statement !=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
*删除一条记录
public static void main(String[] args){
Connection conn = null;
Statement stat = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "111111");
stat = conn.createStatement();
String sql = "delete from student where id =8";
int i = stat.executeUpdate(sql);
if (i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
if (stat != null){
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
4.ResultSet:结果集对象
5.PreparedStatement:执行SQL的对象