A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MoonWarrior 初级黑马   /  2018-12-6 13:47  /  464 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

* 步骤:
                        1. 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
                                1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下
                                2.右键-->Add As Library
                        2. 注册驱动
                        3. 获取数据库连接对象 Connection
                        4. 定义sql
                        5. 获取执行sql语句的对象 Statement
                        6. 执行sql,接受返回结果
                        7. 处理结果
                        8. 释放资源

                * 代码实现:
                          //1. 导入驱动jar包
                //2.注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                //3.获取数据库连接对象
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root");
                //4.定义sql语句
                String sql = "update account set balance = 500 where id = 1";
                //5.获取执行sql的对象 Statement
                Statement stmt = conn.createStatement();
                //6.执行sql
                int count = stmt.executeUpdate(sql);
                //7.处理结果
                System.out.println(count);
                //8.释放资源
                stmt.close();
                conn.close();
Statement stmt = null;
                                Connection conn = null;
                                try {
                                    //1. 注册驱动
                                    Class.forName("com.mysql.jdbc.Driver");
                                    //2. 定义sql
                                    String sql = "insert into account values(null,'王五',3000)";
                                    //3.获取Connection对象
                                    conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
                                    //4.获取执行sql的对象 Statement
                                    stmt = conn.createStatement();
                                    //5.执行sql
                                    int count = stmt.executeUpdate(sql);//影响的行数
                                    //6.处理结果
                                    System.out.println(count);
                                    if(count > 0){
                                        System.out.println("添加成功!");
                                    }else{
                                        System.out.println("添加失败!");
                                    }
                       
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }finally {
                                    //stmt.close();
                                    //7. 释放资源
                                    //避免空指针异常
                                    if(stmt != null){
                                        try {
                                            stmt.close();
                                        } catch (SQLException e) {
                                            e.printStackTrace();
                                        }
                                    }
                       
                                    if(conn != null){
                                        try {
                                            conn.close();
                                        } catch (SQLException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马