任务一: 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

会当凌绝顶,一览众山小
原文地址:https://www.cnblogs.com/quenvpengyou/p/14208611.html