jdbc知识点和代码块


jdbc


思路

六个步骤 注册驱动 获取连接 获取数据库操作对象 执行SQL 处理查询结果集 释放资源
localhost 和 127.0.0.1 都是 本机IP地址 每台机器都是 是本机的回送地址 指本地机 一般用来测试使用
连接数据库存数据
大佬总结更为详细

package com.lianxi;


import java.sql.*;

public class JDBCTest01 {
   
    public static void main(String[] args) {
   
        Statement statement=null;
        Connection connection=null;
        try {
   
            //注册驱动
            Driver driver=new com.mysql.cj.jdbc.Driver();//接口   实现类
            DriverManager.registerDriver(driver);//驱动管理器 静态方法
            //获取连接
            String url="jdbc:mysql://127.0.0.1:3306/bjpowernode?serverTimezone=Asia/Shanghai";//协议 IP地址 端口 数据库下的表
            String user="root";
            String password="159357";
             connection=DriverManager.getConnection(url,user,password);//静态方法 返回的是Connection 连接
            System.out.println("数据库连接对象="+connection);
            //获取数据库操作对象,statement 专门执行sql语句
             statement=connection.createStatement();
            //执行sql
            String sql="insert into dept(deptno,dname,loc) values(50,'人事部','北京')";
            int count=statement.executeUpdate(sql);
            System.out.println(count==1?"保存成功":"保存失败");
            //处理查询结果集
        } catch (SQLException e) {
   
            e.printStackTrace();
        }finally {
   
            //释放资源 为了保证资源一定释放,在Finally 语句块中关闭资源  并且要遵循从小到大依次关闭 分别对其try  ...catch
           if(statement!=null){
   
               try {
   
                   statement.close();
               } catch (SQLException e) {
   
                   e.printStackTrace();
               }
           }
            if(connection!=null){
   
                try {
   
                    connection.close();
                } catch (SQLException e) {
   
                    e.printStackTrace();
                }
            }
        }
    }
}

写sql语句的时候不需要写分号,会报错

第二种写法

package com.lianxi;


import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class JDBCTest01 {
   
    public static void main(String[] args) {
   
       //另一种写法 注册驱动
//        Locale locale=Locale.getDefault();
        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");//资源绑定器  读取配置文件    相对路径需要放在src下
        String driver=bundle.getString("driver");
        String url=bundle.getString("url");
        String user=bundle.getString("user");
        String password=bundle.getString("password");
        Connection connection=null;
        Statement statement=null;

        try {
   


            //注册驱动的第二种方式,常用 因为参数是一个字符串,字符串可以写到xxx.properties文件中  不用接收返回值 因为我们只想用他的类加载动作
            Class.forName(driver);

            connection=DriverManager.getConnection(url,user,password);
            statement=connection.createStatement();
            String sql="insert into dept1 (deptno,dname,loc)values (60,'人事','上海')";
            int count=statement.executeUpdate(sql);
            System.out.println(count==1?"成功":"失败");

        } catch (ClassNotFoundException e) {
   
            e.printStackTrace();
        } catch (SQLException e) {
   
            e.printStackTrace();
        }finally {
   
          if(statement!=null){
   
              try {
   
                  statement.close();
              } catch (SQLException e) {
   
                  e.printStackTrace();
              }
          }
            if(connection!=null){
   
                try {
   
                    connection.close();
                } catch (SQLException e) {
   
                    e.printStackTrace();
                }
            }
        }
        //反射机制 直接加载类 类里有static 已经实现  DriverManager.registerDriver(driver); 要用static静态代码块 就直接加载类即可
    }
}

配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bjpowernode?serverTimezone=Asia/Shanghai
user=root
password=159357

处理结果集

package com.lianxi;


import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class JDBCTest01 {
   
    public static void main(String[] args) {
   
       //另一种写法 注册驱动
//        Locale locale=Locale.getDefault();
        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");//资源绑定器  读取配置文件    相对路径需要放在src下
        String driver=bundle.getString("driver");
        String url=bundle.getString("url");
        String user=bundle.getString("user");
        String password=bundle.getString("password");
        Connection connection=null;
        Statement statement=null;
        ResultSet count=null;

        try {
   


            //注册驱动的第二种方式,常用 因为参数是一个字符串,字符串可以写到xxx.properties文件中  不用接收返回值 因为我们只想用他的类加载动作
            Class.forName(driver);

            connection=DriverManager.getConnection(url,user,password);
            statement=connection.createStatement();
            String sql="select empno as a,ename,sal from emp";
           count=state
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值