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. 执行sqlString 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: 1name: 张三pwd: 123456===================id: 2name: 李四pwd: 122222===================id: 3name: 王超pwd: 133333===================id: 4name: 马汉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(); //执行任何sqlstatement.executeUpdate(); //更新 插入 删除 返回受影响的行数
//5. 执行sqlString 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+类型 不知道用objectresultset.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();}}}}
