本章记录了jdbc的简单使用方式!
0,jdbc的作用
1,jdbc入门准备工作
2,jdbc注册驱动
3,使用jdbc对数据库CRUD
0,jdbc的作用
与数据库建立连接、发送操作数据库的语句并处理得到的结果。
1,jdbc入门准备工作
0),使用jdbc操作mysql所用到jar
mysql-connector-java-5.1.22-bin.jar
1),创建数据库qogir 用户名root 密码root
创建表use
2,jdbc注册驱动
0),注册mysql驱动(3种方式)
注册了数据库驱动,我们才能与数据库建立连接并对数据库做读写的操作。
因为驱动只需要注册一次,所以我们在静态代码块中注册它
第一种方式:使用驱动关联类来注册驱动,它的缺点是当替换数据库(比如:将mysql改为oracle)时,去除mysql的jar后,程序就会出现编译问题。
static {
try {
java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (SQLException e) {
e.printStackTrace();
}
}
查看registerDriver方法的源码:我们可以发现,驱动实际被封装到一个DriverInfo中,然后置于一个Vector之中。
第二种方式:推荐使用这种方式,方便灵活配置驱动
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
使用Class.forName加载驱动类,驱动类在被加载初始化时真正注册驱动
查看com.mysql.jdbc.Driver源码:可以发现,它实际还是使用了第一种方式来注册驱动
第三种方式:将描述驱动的键值对设置到系统属性中
static {
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
}
使用这种方式可以注册多个驱动,多个驱动以“:”分隔
比如加载 mysql 和 oracle 的驱动
System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver:oracle.jdbc.driver.OralceDriver");
在使用DriverManager获取数据库连接时,DriverManager被加载初始化
DriverManager.getConnection(url,username,password);
由源代码看到第三种方式还是使用第二种方式注册驱动,则最终还是使用第一种方式进行驱动注册。
3,使用jdbc对数据库CRUD
1),注册驱动,如上所述
2),获取数据库连接
这个url格式:jdbc:子协议:子名称//主机名:端口/数据库名
这里没有子名称, 若数据库在本地且端口号是默认端口号,则可省略localhost:3306,直接写为 jdbc:mysql:///qogir
private static String url = "jdbc:mysql://localhost:3306/qogir";
private static String username = "root";
private static String password = "root";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
3),创建用于在已经建立数据库连接的基础上,向数据库发送要执行的不带参数的简单SQL语句的工具类对象
Statement statement = null;
statement = connection.createStatement();
4),向数据库发送SQL语句,获取结果集
ResultSet resultSet = null;
resultSet = statement.executeQuery("select id,name,password from user ");
5),处理结果集
while(resultSet.next()){
System.out.println(resultSet.getString("id")+"---->"+resultSet.getString("name")+"--->"+resultSet.getString("password"));
}
6),释放资源,释放资源的顺序应该按照释放结果集,释放Statement,释放数据库连接的顺序
并且,为了不占用太多的资源,数据库连接建立到释放的时间应该尽量短。
public static void close(ResultSet rs,Statement st,Connection con){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null){
try{
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try{
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
最后得到结果
了解了JDBC的读操作,再了解JDBC的写操作应该很容易,具体参考以下完整的程序
package jdbc; import java.sql.*; public final class JdbcUtils { private JdbcUtils(){} private static String url = "jdbc:mysql://localhost:3306/qogir";
private static String username = "root";
private static String password = "root"; /**
* 注册驱动 方式 1 离开驱动jar ,无法编译
*/
/*
static {
try {
java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (SQLException e) {
e.printStackTrace();
}
}*/ /**
* 注册驱动 方式 3 装载com.mysql.jdbc.Driver 推荐的方式
*/
/*
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}*/ /**
* 注册驱动 方式 2
* 多个驱动 以“:”分隔
* System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver:oracle.jdbc.driver.OralceDriver");
*/
static {
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
} /**
* 建立数据库连接
* jdbc:子协议:子名称//主机名:端口/数据库名 这里没有子名称
* 本机,端口号是默认端口号 则可省略"localhost:3306" jdbc:mysql:///qogir
**/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
} /**
* 释放资源
*/
public static void close(ResultSet rs,Statement st,Connection con){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null){
try{
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try{
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
package jdbc; import java.sql.*; public class JdbcTest { /**
* 创建user
*/
public void createUsers(){
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
for(int i=1;i<=10;i++){
String sql = "insert into user(id,name,password) values ("+i+",'name"+i+"','00"+i+"')";
statement.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(null,statement,connection);
}
} /**
* 更新user
*/
public void updateUsers(){
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
for(int i=1;i<=10;i++){
String sql = "update user set name='abc"+i+"' where id="+i;
statement.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(null,statement,connection);
}
} /**
* 删除user
*/
public void deleteUsers(){
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
String sql = "delete from user where 1=1 ";
int i = statement.executeUpdate(sql);
System.out.println("删除了"+i+"条数据!");
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(null,statement,connection);
}
} /**
* 读user
*/
public void readUsers(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
/**
* 2,建立数据库连接
*/
connection = JdbcUtils.getConnection();
/**
* 3,创建用于在已经建立数据库连接的基础上,向数据库发送要执行的不带参数的简单SQL语句的工具类对象
*/
statement = connection.createStatement();
/**
* 4,向数据库发送SQL语句,获取结果集
*/
resultSet = statement.executeQuery("select id,name,password from user ");
/**
* 5,处理结果
*/
while(resultSet.next()){
System.out.println(resultSet.getString("id")+"---->"+resultSet.getString("name")+"--->"+resultSet.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
/**
* 6,释放资源
*/
JdbcUtils.close(resultSet,statement,connection);
}
} public static void main(String[] args){ //new JdbcTest().createUsers();
//new JdbcTest().updateUsers();
//new JdbcTest().deleteUsers();
new JdbcTest().readUsers();
} }
- 本文来自:Linux教程网