JDBC学习——简单学习JDBC
1.初始JDBC
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
简单点来说,JDBC就是Java用来操作数据库的一套接口。
2.JDBC下载和搭建
地址: https://mvnrepository.com/artifact/mysql/mysql-connector-java
点击下载与自己数据库版本相应的jar包
然后将下载好的jar包导入到idea中,构建好即可
3.JDBC步骤
① 加载驱动
② 获取与数据库的链接
③ 获取用于向数据库发送sql语句的statement
④ 向数据库发sql,并获取代表结果集的resultset
⑤ 取出结果集的数据
⑥ 关闭链接,释放资源
public class JDBC1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
String username = "root";
String password ="123456";
String url = "jdbc:mysql://localhost:3306/jdbcstudy";
Connection connection = DriverManager.getConnection(url,username,password);
//3.编写SQL语句,获取向数据库发送sql语句的statement对象
String sql = "select id,name,password,email from users";
Statement statement = connection.createStatement();
//4.提交sql语句,返回值为一个结果集:executeQuery(查询)、executeUpdate(增删改)
ResultSet resultSet = statement.executeQuery(sql);
//5.循环打印结果集数据
while (resultSet.next()){
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("name"));
System.out.println(resultSet.getObject("password"));
System.out.println(resultSet.getObject("email"));
}
//6.释放资源
resultSet.close();
statement.close();
connection.close();
}
}
4.JDBC工具类的编写
首先,新建配置文件:jdbc.properties,然后再文件中添加:
driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/jdbcstudy
再将一些固定代码写到工具类中:
public class JDBCUtils {
private static String url;
private static String username;
private static String password;
private static String qd;
//使用静态代码块是为了在工具类加载时自动加载静态代码块内的代码,而不用手动操作
static {
//获取配置恩建中的内容
Properties properties = new Properties();
try {
properties.load(new FileReader("jdbc.txt"));
} catch (IOException e) {
e.printStackTrace();
}
qd = properties.getProperty("qd");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
try {
Class.forName(qd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
}
//释放资源
public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
if (connection!=null){
connection.close();
}
if (statement!=null){
statement.close();
}
if (resultSet!=null){
resultSet.close();
}
}
public static void close(Connection connection, Statement statement) throws SQLException {
if (connection!=null){
connection.close();
}
if (statement!=null){
statement.close();
}
}
}
然后测试:
public class JDBCDemo {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into users values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,"saaaass");
statement.setString(2,"654321");
int i = statement.executeUpdate();
if (i>0){
System.out.println("插入成功");
}else {
System.out.println("失败");
}
JDBCUtils.close(connection,statement);
}
}