JDBC学习笔记
案例
import java.sql.*;
public class JDBCTest01 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
try {
//1、注册驱动
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型引用指向子类型对象。
DriverManager.registerDriver(driver);
//2、获取连接
/*
* url:统一资源定位符(网络中某个资源的绝对路径)
* url包括哪几部分?
* 协议
* IP
* PORT 端口
* 资源名
*
* http://182.61.200.7:80/index.html
* http://通信协议
* 182.61.200.7 服务器IP地址
* 80 服务器上软件的端口
* index.html 是服务器上某个资源名
*
* jdbc:mysql://127.0.0.1:3306/test
* jdbc:mysql://协议
* 127.0.0.1 IP地址
* 3306 mysql数据库端口号
* test 具体的数据库实例名
*
* 说明:localhost和127.0.0.1都是本机IP地址。
*
* 什么是通信协议,有什么用?
* 通信协议是通信之前就提前定好的数据传送格式。
* 数据包具体怎么传数据,格式提前定好的。
*
*
* */
String url = “jdbc:mysql://127.0.0.1:3306/test”;
String user =“root”;
String password = “123456”;
conn =DriverManager.getConnection(url,user,password);
System.out.println(“数据库连接对象=” + conn);
//3、获取数据库操作对象(Statement专门执行sql语句的)
stmt = conn.createStatement();
//4、执行sql
//JDBC中的sql语句不需要提供分号结尾,否则会报错
String sql ="insert into account values(10,null,null)";
int count=stmt.executeUpdate(sql);
System.out.println(count==1 ? "保存成功":"保存失败");
//5、处理查询结果集
}catch (SQLException e){
e.printStackTrace();
}finally {
//6、释放资源
//为了保证资源一定释放,在finally语句块中关闭资源
//并且要遵循从小到大依次关闭
//分别对其try..catch
try{
if(stmt !=null){
stmt.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if(conn !=null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
第二种驱动注册方法 常用
public class JDBCTest03 {
public static void main(String[] args) {
try {
//1、注册驱动
//这是注册驱动的第一种写法
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//注册驱动的第二种方式:常用的。
//为什么这种方式常用?因为参数是一个字符串,字符串可以写到xxx.properties文件中。
//以下方法不需要接收返回值,因为我们只想用它的类加载动作。
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
System.out.println(conn);
} catch (SQLException e) {
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
//将连接数据库的所有信息配置到配置文件中
/*
- 实际开发中不建议把连接数据库的信息写死到Java程序中。
- /
import java.sql.;
import java.util.ResourceBundle;
public class JDBCTest04 {
public static void main(String[] args) {
//使用资源绑定器绑定属性配置文件
ResourceBundle bundle =ResourceBundle.getBundle("jdbc");
String driver =bundle.getString("driver");
String url =bundle.getString("url");
String user =bundle.getString("user");
String password =bundle.getString("password");
Connection conn=null;
Statement stmt=null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn =DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象=" + conn);
//3、获取数据库操作对象(Statement专门执行sql语句的)
stmt = conn.createStatement();
//4、执行sql
//JDBC中的sql语句不需要提供分号结尾
String sql ="update account set username ='xf' where id=10";
int count=stmt.executeUpdate(sql);
System.out.println(count==1 ? "修改成功":"修改失败");
//5、处理查询结果集
}catch (Exception e){
e.printStackTrace();
}finally {
//6、释放资源
//为了保证资源一定释放,在finally语句块中关闭资源
//并且要遵循从小到大依次关闭
//分别对其try..catch
try{
if(stmt !=null){
stmt.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if(conn !=null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
配置文件
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
user=root
password=123456