JDBC是java提供一套用于数据库操作的接口API,java程序员只需要面向这套接口编程即可,不同的数据库厂商,需要针对这套接口,提供不同的实现
一、编写一个简单的JDBC程序,连接数据库
1.添加符合mysql版本的mysql-connector-java-bin.jar包
jar包下载地址:jar包下载地址(选择符合本机mysql版本的jar包)
导入eclipse的方法:在项目中新建一个名字为libs的文件夹,解压下载的符合版本的jar包,复制mysql-connector-java-bin.jar,粘贴到新建的libs文件夹中,右键点击mysql-connector-java-bin.jar,选择Bulid Path->Configure Bulid Path,点击libraries,选择Add JARS...,点击Apply and Close完成添加。
2.注册驱动-加载Driver类
Driver driver=new com.mysql.jdbc.Driver();//创建driver对象
3.获取链接-得到Connection
String url="jdbc:mysql://localhost:3306/db01";
//将用户名和密码放入到Properties对象
Properties properties=new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "fly");
Connection connect=driver.connect(url, properties);
4.执行增删改查-发送SQL给mysql执行
String sql="insert into actor values(null,'刘德华','男','1970-11-11','110')";
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);//如果是dml语句,返回的就是影响行数
System.out.println(rows>0?"成功":"失败");
5.释放资源-关闭相关连接
statement.close();
connect.close();
6.完整代码
package com.fly.jdbc.myjdbc;
import java.sql.*;
import java.util.Properties;
import com.mysql.jdbc.Driver;
public class Jdbc01 {
public static void main(String[] args) throws SQLException {
//前置工作:在项目下创建一个文件夹jdbc,将jar包放在里面,并添加到项目中
//1.注册驱动
Driver driver=new com.mysql.jdbc.Driver();//创建driver对象
//2.得到连接
String url="jdbc:mysql://localhost:3306/db01";
//将用户名和密码放入到Properties对象
Properties properties=new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "fly");
Connection connect=driver.connect(url, properties);
//3.执行sql;
String sql="insert into actor values(null,'刘德华','男','1970-11-11','110')";
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);//如果是dml语句,返回的就是影响行数
System.out.println(rows>0?"成功":"失败");
//4.关闭连接资源
statement.close();
connect.close();
}
}
7.运行结果
在eclipse中运行后,可以在数据库图形化文件SQLyog中看到如下结果
二、获取数据库连接的五种方式
1.方式1
public void connect01() throws SQLException
{
Driver driver=new com.mysql.jdbc.Driver();//创建driver对象
String url="jdbc:mysql://localhost:3306/db01";
Properties properties=new Properties();
properties.setProperty("user", "your username");
properties.setProperty("password", "your password");
Connection connect=driver.connect(url, properties);
System.out.println("方式1"+connect);
}
这种方式属于静态加载,灵活性比较差,依赖强,所以推出方式2
2.方式2
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
{
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver =(Driver)aClass.newInstance();
String url="jdbc:mysql://localhost:3306/db01";
Properties properties=new Properties();
properties.setProperty("user", "your username");
properties.setProperty("password", "your password");
Connection connect=driver.connect(url, properties);
System.out.println("方式2"+connect);
}
3.方式3
使用DriverManager替代Driver
public void connect03() throws IllegalAccessException,InstantiationException,ClassNotFoundException, SQLException
{
Class<?> aClass =Class.forName("com.mysql.jdbc.Driver");
Driver driver =(Driver) aClass.newInstance();
String url="jdbc:mysql://localhost:3306/db01";
String user="your username";
String password="your password";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式三"+connection);
}
4.方式4
//使用Class.forName自动完成注册驱动,简化代码
//甚至可以去掉Class.forName("com.mysql.jdbc.Driver");这条语句,因为在静态代码块中默认可以注册驱动(建议还是写上)
public void connect04() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/db01";
String user="your username";
String password ="your password";
Connection connection=DriverManager.getConnection(url, user, password);
System.out.println("方式4"+connection);
}
5.方式5
编写配置文件,如下:(src目录右键,新建一个file 文件,取名为mysql.properties),
user=root
password=fly
url=jdbc:mysql://localhost:3306/db01
driver=com.mysql.jdbc.Driver
代码如下:
public void connect05() throws IOException, ClassNotFoundException, SQLException
{
//通过properties对象获取配置文件的信息
Properties properties =new Properties();
properties.load(new FileInputStream("src\\mysql.properities"));
//获取相关的值
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("方法5"+connection);
}