• 任务一: 2、JDBC


            
            //完整版的jdbc连接数据库的代码,常用配置文件实现的
            import java.sql.*;
            import java.util.*;
            
            public class JDBCtest04{
                public static void main(String [] args){
                    
                    //使用资源绑定器绑定属性配置文件
                    ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
                    String driver = dundle.getString("driver");
                    String url = dundle.getString("url");
                    String password = dundle.getString("passwod");
                    
                    
                    Connection conn;
                    Statement stmt;
                    try{
                        //1,注册驱动
                        Class.forname(driver);
                        //2,获取连接
                        conn = driverManager.getConnection(url,user,password);
                        //3,获取数据库操作对象
                        stmt = conn.createStatement();
                        
                        //4,执行sql语句
                        String sql = "update dept set dno ='xxx'";
                        
                        int count = stmt.executeUpdate(sql);
                        
                        //5,如果第四步是查找的sql语句的话,在执行这个
                        }catch(SQLException e){
                            e.printStackTrace();
                            }
                            finally{
                        //6,释放资源,要分别释放资源,从小到大
                        if(stmt != null){
                        try{
                            stmt.close();
                            }catch(SQLException e){
                                e.printStackTrace();
                                }
                                
                                
                                if(conn != null){
                                    try{
                                        conn.close();
                                        }catch(SQLException e){
                                            e.printStackTrace();
                                            }
                        }

    我的配置文件名:jdbc.properties

      内容:driver=xxxxx

         url=xxxxx

         user=xxxx

         password=xxxx

    会当凌绝顶,一览众山小
  • 相关阅读:
    [luogu p1996] 约瑟夫问题
    [luogu p1098] 字符串的展开
    [luogu p1035] 级数求和
    [luogu p1004] 方格取数
    [luogu p3383]【模板】线性筛素数
    [luogu p1223] 排队接水
    [luogu p1002] 过河卒
    [luogu p1001] A+B Problem
    Java BIO/NIO(Non-blocking I/O)详解
    Linux页框&伙伴算法以及slab机制
  • 原文地址:https://www.cnblogs.com/quenvpengyou/p/14208611.html
Copyright © 2020-2023  润新知