ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JDBC-Connecion详解注册驱动和Statement详解注册驱动

2022-07-28 16:36:32  阅读:132  来源: 互联网

标签:String throwables stmt 详解 注册 sql 驱动 null conn


Connecion详解注册驱动

Connection:数据库连接对象

功能:

  • 获取执行sql的对象
Statement createStatement();
PreparedStatement PrepareStatement(String sql)
  • 管理事务
开启事务:setAutoCommit(boolean autocommit):调用该方法设置参数为false 即开启事务
提交事务:Commit();
回滚事务:rollback();

Statement详解注册驱动

statement:执行sql的对象

功能

1.boolean execute(String sql):可以执行任意的sql 
2.int ececuteUpdate(String sql):执行DML(insert、update、delete)语句、DDL(create,alter,drop)语句
返回值:影响的行数 可以通过这个影响的行数判断DML语句是否执行成功 返回值>0的则执行成功 反之 则失败
3.ResultSet executeQuery(String sql):执行DQL(select)语句

 练习

1.account表 添加一条记录

代码

 public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //定义SQL语句
            String sql = "INSERT INTO account values(null,'王五',3000)";
            //获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///videopractice", "root", "root");
            //获取执行SQL的对象 statement
            stmt = conn.createStatement();
            //执行sql
            int count = stmt.executeUpdate(sql);//影响的行数
            //处理结果
            System.out.println(count);
            if (count > 0) {
                System.out.println("添加成功!");
            } else {
                System.out.println("添加失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
    }

运行结果

 

 

 SQL数据表

 

 

 

 2.account表 删除一条记录

 数据表

 

 代码

 

 public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //定义SQL语句
            String sql = "delete from account where id=4";
            //获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///videopractice", "root", "root");
            //获取执行SQL的对象 statement
            stmt = conn.createStatement();
            //执行sql
            int count = stmt.executeUpdate(sql);//影响的行数
            //处理结果
            System.out.println(count);
            if (count > 0) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
    }

运行结果

 

 数据库

 

标签:String,throwables,stmt,详解,注册,sql,驱动,null,conn
来源: https://www.cnblogs.com/aimz01/p/16529100.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有