Java文件操作、jdbc增删查改操作

java文件操作

重命名文件

 File file1=new File("D:\\bbb.txt");
        file.renameTo(file1);*/

打印目录下文件名

String path="D:\\a";
        File file4=new File(path);
        File[] arr=file4.listFiles();
        for(int i=0;i<arr.length;i++){//打印文件名
            System.out.println(arr[i].getName());
        }

在文件中写入内容

     FileWriter fw = null;
        fw = new FileWriter(file1);
        fw.write("hello  我好开心,作业很少");
        fw.flush();

jdbc增删查改操作

DBUtil类用于整合jdbc连接的步骤

public class DBUtil {
    public static Connection getConnection(){
        Connection connection=null;
        //1.加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            connection = DriverManager.getConnection
                    ("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
            System.out.println("创建连接成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeAll(ResultSet rs, PreparedStatement preparedStatement,Connection connection){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}

查找操作

public class TestSelect {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        DBUtil dbUtil=null;

        //1.加载驱动
        try {
            dbUtil=new DBUtil();
            connection = dbUtil.getConnection();
            //3.写sql
            String sql="select *from student  where id=?";
            //4.获得statement对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            preparedStatement.executeQuery();


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dbUtil.closeAll(null,preparedStatement,connection);
        }

    }
}

删除操作

public class TestDelete {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        DBUtil dbUtil=null;

        //1.加载驱动
        try {
            dbUtil=new DBUtil();
            connection = dbUtil.getConnection();
            //3.写sql
            String sql="delete from student where id=?";
            //4.获得statement对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,3);
            preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
           dbUtil.closeAll(null,preparedStatement,connection);
        }

    }
}

新增操作

public class TestInsert {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        DBUtil dbUtil=null;

        //1.加载驱动
        try {
            dbUtil=new DBUtil();
            connection = dbUtil.getConnection();
            //3.写sql
            String sql="insert into student (id,name,password) values(?,?,?)";
            //4.获得statement对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            preparedStatement.setString(2, "lizhiyang");
            preparedStatement.setString(3,"shisb");
            preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dbUtil.closeAll(null,preparedStatement,connection);
        }

    }
}

修改操作

public class TestUpdate {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        DBUtil dbUtil=null;

        //1.加载驱动
        try {
            dbUtil=new DBUtil();
            connection = dbUtil.getConnection();
            //3.写sql
            String sql="update student set password =? where id=?";
            //4.获得statement对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,123456);
            preparedStatement.setInt(2,1);
            preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dbUtil.closeAll(null,preparedStatement,connection);
        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值