JDBC连接Mysql数据库

JDBC连接数据库

import java.sql.*;

public class Test01 {
   
    public static void main(String[] args) {
   
        Connection connection=null;
        Statement statement=null;

        ResultSet rs=null;

        //1.加载驱动
        try {
   
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接对象
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3307/j3071test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8","root","root");
            //3.创建语句查询
            statement=connection.createStatement();
//            //4.执行语句并且返回结果
//            String sql="select * from student where age=22";
//            rs= statement.executeQuery(sql);
//            //5.遍历结果集
//            while(rs.next()){
   
//                //获取需要的字段
//                String name = rs.getString("name");
//                int id = rs.getInt("id");
//                System.out.println("id:"+id+",Name:"+name);
//            }

//          修改语句
            String sql="insert into student (id,name,age,sex) values(id,'j3071',20,'男')";
            //字符串拼接
            String str1="insert into student (id,name,age,sex) values(id,";
            String name="张三";
            int age=20;
            String sql1=str1+"'"+name+"',"+age+","+"'男')";
            int row = statement.executeUpdate(sql);//返回值是影响的行数
            //增删改的语句都用executeUpdate
            System.out.println("受影响的行数为:"+row);

        } catch (ClassNotFoundException e) {
   
            e.printStackTrace();
        } catch (SQLException throwables) {
   
            throwables.printStackTrace();
        }finally {
   
            try {
   
                if (rs!=null){
   
                    rs.close();
                }
                if (statement!=null){
   
                    statement.close();
                }
                if (connection!=null){
   
                    connection.close();
                }
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
    }
}

相关推荐

  1. JDBC连接Mysql数据库

    2023-12-25 08:06:04       31 阅读
  2. JDBC数据库连接

    2023-12-25 08:06:04       38 阅读
  3. JDBC数据库连接

    2023-12-25 08:06:04       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 08:06:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 08:06:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 08:06:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 08:06:04       18 阅读

热门阅读

  1. 将ncnn及opencv的mat存储成bin文件的方法

    2023-12-25 08:06:04       37 阅读
  2. [英语单词] piss in the wind

    2023-12-25 08:06:04       39 阅读
  3. 利用std::unique_ptr释放资源

    2023-12-25 08:06:04       37 阅读
  4. 喝酒、工作与自律

    2023-12-25 08:06:04       39 阅读
  5. 设计循环队列

    2023-12-25 08:06:04       44 阅读
  6. Milvus向量数据库基础用法及注意细节

    2023-12-25 08:06:04       46 阅读
  7. 力扣56.合并区间

    2023-12-25 08:06:04       33 阅读
  8. ARM AArch64的TrustZone架构详解(上)

    2023-12-25 08:06:04       35 阅读
  9. 阿里云公有云平台

    2023-12-25 08:06:04       35 阅读
  10. Iceberg: COW模式下的MERGE INTO的执行流程

    2023-12-25 08:06:04       35 阅读
  11. 设计模式之工厂方法模式

    2023-12-25 08:06:04       41 阅读
  12. 手写爬虫框架

    2023-12-25 08:06:04       45 阅读
  13. FFmpeg常见命令行

    2023-12-25 08:06:04       37 阅读
  14. Vue3和Vue2的区别

    2023-12-25 08:06:04       32 阅读