数据库驱动
驱动:声卡,显卡,数据库
我们的程序会通过数据库驱动,和数据库打交道!
JDBC
SUN 公司为了简化开发人员的(对数据库的统一)操作,提供了一个(Java操作数据库的)规范,JDBC
这些规范的实现由具体的厂商去做
对于开发人员来说,我们只需要掌握JDBC的接口操作即可
第一个JDBC程序
创建数据库
CREATE DATABASE `jdbcstudy` ; use `jdbcstudy` CREATE TABLE `users` ( `id` int NOT NULL, `NAME` varchar(40) DEFAULT NULL, `PASSWORD` varchar(40) DEFAULT NULL, `email` varchar(60) DEFAULT NULL, `birthday` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users`(id,NAME,PASSWORD,email,birthday) VALUES(1,'zhansan','123456','zs@sina.com','1980-12-04'), (2,'lisi','123456','lisi@sina.com','1981-12-04'), (3,'wangwu','123456','wangwu@sina.com','1979-12-04')
|
1.创建一个普通项目
2.导入数据库驱动(
)
3.编写测试代码
package com.lyx.lesson01;
import java.sql.*;
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEnconding=utf8&useSSL=true&serverTimezone=GMT";
String username = "root";
String password = "312444";
Connection conncetion = DriverManager.getConnection(url,username,password);
Statement statement = conncetion.createStatement();
String sql = "select * from `users`";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){ System.out.println("id = " + resultSet.getObject("id") );
System.out.println("name = " + resultSet.getObject("NAME") );
System.out.println("password = " + resultSet.getObject("PASSWORD") );
System.out.println("email = " + resultSet.getObject("email") );
System.out.println("birthday = " + resultSet.getObject("birthday"));
System.out.println("=============================================="); }
resultSet.close(); conncetion.close(); statement.close(); } }
|
步骤总结:
1.加载驱动
2.连接数据库 DriverManager
3.获取执行SQL的对象 Statement
4.获得返回的结果集
5.释放连接
DriverManager
Class.forName("com.mysql.jdbc.Driver"); Connection connection= DriverManager.getConnection(url,name,password);
connection.rollback(); connection.commit(); connection.setAutoCommit();
|
Url
String url ="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&&useSSL=false";
|
statement 执行SQL的对象 pPrepareStatement 执行SQL的对象
String sql="SELECT * FROM users";
statement.executeQuery(); statement.execute(); statement.executeUpdate();
|
ResultSet 查询的结果集,封装了所以的查询结果
获得指定的数据类型
ResultSet resultSet = statement.executeQuery(sql); resultSet.getObject(); resultSet.getString(); resultSet.getInt();
|
遍历,指针
resultSet.next(); resultSet.afterLast(); resultSet.beforeFirst(); resultSet.previous(); resultSet.absolute(row);
|
释放内存
resultSet.close(); statement.close(); connection.close();
|
JDBC程序最终版(配置文件)
在src目录下新建一个file.properties
user=root password=123456 url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEnconding=utf8&useSSL=true&serverTimezone=GMT driverClass=com.mysql.cj.jdbc.Driver
|
编写类FinalConnect
package com.lyx.lesson01;
import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties;
public class FinalConnect { public static void main(String[] args) throws Exception { InputStream is = FinalConnect.class.getClassLoader().getResourceAsStream("file.properties"); Properties properties = new Properties(); properties.load(is); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); String driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
} }
|