JDBC
import java.sql.*;
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver"); // 固定写法 加载驱动 内部源码就是一个静态代码块
// 执行就行
// 2. url 账号密码
String url = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";
// 3. 连接成功,数据库对象
Connection connection = DriverManager.getConnection(url, username, password);
// 4. 执行sql语句的对象
Statement statement = connection.createStatement();
//5. 执行sql
String sql = "SELECT * FROM user";
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("pwd: "+resultSet.getObject("pwd"));
System.out.println("===================");
}
//6. 释放对象
resultSet.close();
statement.close();
connection.close(); // 消耗资源,用完关掉
}
}
id: 1
name: 张三
pwd: 123456
===================
id: 2
name: 李四
pwd: 122222
===================
id: 3
name: 王超
pwd: 133333
===================
id: 4
name: 马汉
pwd: 144444
===================
Process finished with exit code 0
url
String url = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false";
//jdbc:mysql://主机:端口/数据库名?参数1&参数2&参数3
// 3. 连接成功,数据库对象
Connection connection = DriverManager.getConnection(url, username, password);
// connection代表数据库 事务提交 事务回滚
// 4. 执行sql语句的对象
Statement statement = connection.createStatement();
//Statement 具体执行类 执行增删改查语句
statement.executeQuery(); //查询操作 返回结果集
statement.execute(); //执行任何sql
statement.executeUpdate(); //更新 插入 删除 返回受影响的行数
//5. 执行sql
String sql = "SELECT * FROM user";
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("pwd: "+resultSet.getObject("pwd"));
System.out.println("===================");
}
resultset.getString(); // 知道具体类型 就用get+类型 不知道用object
resultset.getInt();
Statement对象
statement对象用于向数据库发送SQL命令。
提取工具类
package com.angyi.utils;
import javax.swing.plaf.nimbus.State;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String url=null;
private static String driver=null;
private static String pwd=null;
private static String username=null;
static{
InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
url = properties.getProperty("jdbc.url");
driver = properties.getProperty("jdbc.driver");
pwd = properties.getProperty("jdbc.password");
username = properties.getProperty("jdbc.username");
//1。 驱动只需要加载一次
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Connection connection = DriverManager.getConnection(url,username,pwd);
return connection;
}
public static void release(Connection con, Statement st,ResultSet rs) throws SQLException {
if(con!=null){
try{
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(st!=null){
try{
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(rs!=null){
try{
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}