JDBC

  1. import java.sql.*;
  2. public class JdbcFirstDemo {
  3. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  4. // 1. 加载驱动
  5. Class.forName("com.mysql.jdbc.Driver"); // 固定写法 加载驱动 内部源码就是一个静态代码块
  6. // 执行就行
  7. // 2. url 账号密码
  8. String url = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false";
  9. String username = "root";
  10. String password = "123456";
  11. // 3. 连接成功,数据库对象
  12. Connection connection = DriverManager.getConnection(url, username, password);
  13. // 4. 执行sql语句的对象
  14. Statement statement = connection.createStatement();
  15. //5. 执行sql
  16. String sql = "SELECT * FROM user";
  17. ResultSet resultSet = statement.executeQuery(sql);
  18. while(resultSet.next()){
  19. System.out.println("id: "+resultSet.getObject("id"));
  20. System.out.println("name: "+resultSet.getObject("name"));
  21. System.out.println("pwd: "+resultSet.getObject("pwd"));
  22. System.out.println("===================");
  23. }
  24. //6. 释放对象
  25. resultSet.close();
  26. statement.close();
  27. connection.close(); // 消耗资源,用完关掉
  28. }
  29. }
  1. id: 1
  2. name: 张三
  3. pwd: 123456
  4. ===================
  5. id: 2
  6. name: 李四
  7. pwd: 122222
  8. ===================
  9. id: 3
  10. name: 王超
  11. pwd: 133333
  12. ===================
  13. id: 4
  14. name: 马汉
  15. pwd: 144444
  16. ===================
  17. Process finished with exit code 0

url

  1. String url = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false";
  2. //jdbc:mysql://主机:端口/数据库名?参数1&参数2&参数3
  1. // 3. 连接成功,数据库对象
  2. Connection connection = DriverManager.getConnection(url, username, password);
  3. // connection代表数据库 事务提交 事务回滚
  1. // 4. 执行sql语句的对象
  2. Statement statement = connection.createStatement();
  3. //Statement 具体执行类 执行增删改查语句
  4. statement.executeQuery(); //查询操作 返回结果集
  5. statement.execute(); //执行任何sql
  6. statement.executeUpdate(); //更新 插入 删除 返回受影响的行数
  1. //5. 执行sql
  2. String sql = "SELECT * FROM user";
  3. ResultSet resultSet = statement.executeQuery(sql);
  4. while(resultSet.next()){
  5. System.out.println("id: "+resultSet.getObject("id"));
  6. System.out.println("name: "+resultSet.getObject("name"));
  7. System.out.println("pwd: "+resultSet.getObject("pwd"));
  8. System.out.println("===================");
  9. }
  10. resultset.getString(); // 知道具体类型 就用get+类型 不知道用object
  11. resultset.getInt();

Statement对象

statement对象用于向数据库发送SQL命令。
提取工具类

  1. package com.angyi.utils;
  2. import javax.swing.plaf.nimbus.State;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.sql.*;
  6. import java.util.Properties;
  7. public class JdbcUtils {
  8. private static String url=null;
  9. private static String driver=null;
  10. private static String pwd=null;
  11. private static String username=null;
  12. static{
  13. InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
  14. Properties properties = new Properties();
  15. try {
  16. properties.load(inputStream);
  17. url = properties.getProperty("jdbc.url");
  18. driver = properties.getProperty("jdbc.driver");
  19. pwd = properties.getProperty("jdbc.password");
  20. username = properties.getProperty("jdbc.username");
  21. //1。 驱动只需要加载一次
  22. Class.forName(driver);
  23. } catch (IOException | ClassNotFoundException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. public static Connection getConnection() throws ClassNotFoundException, SQLException {
  28. Connection connection = DriverManager.getConnection(url,username,pwd);
  29. return connection;
  30. }
  31. public static void release(Connection con, Statement st,ResultSet rs) throws SQLException {
  32. if(con!=null){
  33. try{
  34. con.close();
  35. }catch (SQLException e){
  36. e.printStackTrace();
  37. }
  38. }
  39. if(st!=null){
  40. try{
  41. con.close();
  42. }catch (SQLException e){
  43. e.printStackTrace();
  44. }
  45. }
  46. if(rs!=null){
  47. try{
  48. con.close();
  49. }catch (SQLException e){
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54. }