本次博客带领大家快速入门JDBC和获取数据库的连接方式。
JDBC程序编写步骤
- 注册驱动 - 加载Driver 类。
- 获取连接 - 得到Connection。
- 执行增删改查 - 发送SQL 给 mysql 执行。
- 释放资源 - 关闭相关连接。
JDBC第一个程序
- 通过jdbc 对 表actor 进行 添加,删除和修改操作。
-- 先在mysql数据库中创建actor表
CREATE TABLE actor(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32) NOT NULL DEFAULT '',
sex CHAR(1) NOT NULL DEFAULT '女',
borndate DATETIME,
phone VARCHAR(12));
- 实现代码:
public class Jdbc01 {
public static void main(String[] args) throws SQLException {
// 前置工作:在项目下创建一个文件夹比如 libs
// 将mysql.jar 拷贝到该目录下面,点击add to project ..加入到该项目
//1.注册驱动
Driver driver = new Driver();
//2.得到连接
String url = "jdbc:mysql://localhost:3306/ld_db01";
//将 用户名和密码放入到Properties对象
Properties properties = new Properties();
properties.setProperty("user","root");//用户
properties.setProperty("password","root");//密码
Connection connect = driver.connect(url, properties);
//3.执行sql
//String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";
//String sql = "update actor set name='周星驰' where id = 1";
String sql = "DELETE from actor where id = 1";
//Statement用于执行静态SQL语句并返回其生成的结果的对象
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql); //如果是dml语句,返回的就是影响行数。
System.out.println(rows > 0 ? "成功":"失败");
//4.关闭连接资源
statement.close();
connect.close();
}
}
获取数据库连接的5种方式
- 方式1:
public void connect01() throws SQLException {
Driver driver = new Driver();
//得到连接
String url = "jdbc:mysql://localhost:3306/ld_db01";
//将 用户名和密码放入到Properties对象
Properties properties = new Properties();
properties.setProperty("user","root");//用户
properties.setProperty("password","root");//密码
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
- 方式2:方式1会直接使用com.mysql.jdbc.Driver(),属于静态加载,灵活性差,依赖强。
public void connect02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
//使用反射加载Driver类
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/ld_db01";
//将 用户名和密码放入到Properties对象
Properties properties = new Properties();
properties.setProperty("user","root");//用户
properties.setProperty("password","root");//密码
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
- 方式3:使用DriverManager 替代 Diver 进行统一管理。
public void connect03() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
//使用反射加载Driver
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
//创建url和user和password
String url = "jdbc:mysql://localhost:3306/ld_db01";
String user = "root";
String password = "root";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
- 方式4:使用Class.forName 自动完成注册驱动,简化代码。
public void connect04() throws ClassNotFoundException, SQLException {
//使用反射加载了 Driver类
//在加载 Driver类时,完成注册
Class.forName("com.mysql.jdbc.Driver");
//创建url和user和password
String url = "jdbc:mysql://localhost:3306/ld_db01";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
- 方式5:在方式4的基础上改进,增加配置文件,让连接mysql更加灵活。
public void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
user = root
password = root
url = jdbc:mysql://localhost:3306/ld_db01
driver = com.mysql.jdbc.Driver
小练习
-
创建news表。
-
使用jdbc添加5条数据。
-
修改id=1的记录,将content 改成 一个新的消息。
-
删除id = 3的记录。
-- 1.
CREATE TABLE news (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32) NOT NULL DEFAULT '',
content TEXT NOT NULL
)
// 2.
public class JdbcTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql1 = "insert into news values(1,'广州','广州疫情最新情况!')";
String sql2 = "insert into news values(2,'北京','北京疫情最新情况!')";
String sql3 = "insert into news values(3,'上海','上海疫情最新情况!')";
String sql4 = "insert into news values(4,'重庆','重庆疫情最新情况!')";
String sql5 = "insert into news values(5,'汕头','汕头疫情最新情况!')";
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql1);
rows = statement.executeUpdate(sql2);
rows = statement.executeUpdate(sql3);
rows = statement.executeUpdate(sql4);
rows = statement.executeUpdate(sql5);
statement.close();
connection.close();
}
}
// 3.
public class JdbcTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "update news set content='中秋快乐!!!' where id = 1";
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql);
statement.close();
connection.close();
}
}
//4.
public class JdbcTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "DELETE from news where id = 1";
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql);
statement.close();
connection.close();
}
}