1-13JAVA课程学习

1-13JAVA课程学习用JAVA实现Mysql的增删改查新建方法packagecom.NBUFE.util;importjava.sql.*;publicclassDBUtil{publicstaticConnectiongetConnection(){Connectionconnection=null;//加载驱动tr…

用JAVA实现Mysql的增删改查

新建方法

package com.NBUFE.util;

import java.sql.*;

public class DBUtil { 
   
    public static Connection getConnection() { 
   
        Connection connection = null;
        //加载驱动
        try { 
   
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbufe_lxl?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();
            }

        }
    }
}

添加

package com.NBUFE.JDBC;

import com.NBUFE.bean.Student;
import com.NBUFE.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TextInset { 
   
    public static void main(String[] args) throws SQLException { 
   
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "insert into student(id,name,password) values(?,?,?)";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 4);
        preparedStatement.setString(2, "lxl");
        preparedStatement.setString(3, "777");
        System.out.println("添加成功");
        preparedStatement.executeUpdate();
        DBUtil.closeAll(resultSet, preparedStatement, connection);
    }
}

查找

package com.NBUFE.JDBC;

import com.NBUFE.bean.Student;
import com.NBUFE.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Textjdbc { 
   
    public static void main(String[] args) throws Exception { 
   
        Connection connection = DBUtil.getConnection();
        List<Student> students=new ArrayList<>();
        //3.写sql语句
        String sql="select * from student";
        //4.得到statement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //5.执行sql得到结果集
        ResultSet resultSet=preparedStatement.executeQuery();
        //6.处理结果
        while(resultSet.next()){ 
   
            Student student=new Student();
            student.setId(resultSet.getInt(1));
            student.setName(resultSet.getString(2));
            student.setPassword(resultSet.getString(3));
            students.add(student);
        }
        System.out.println(students);
        //7.关闭资源
        DBUtil.closeAll(resultSet,preparedStatement,connection);
    }
}

修改

package com.NBUFE.JDBC;

import com.NBUFE.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TextUpdate { 
   
    public static void main(String[] args) throws SQLException { 
   
        Connection connection = DBUtil.getConnection();
        ResultSet resultSet=null;
        String sql="update student set name=?,password=? where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"zzz");
        preparedStatement.setString(2, "zzz");
        preparedStatement.setInt(3,3);
        System.out.println("修改成功");
        preparedStatement.executeUpdate();
        DBUtil.closeAll(resultSet, preparedStatement, connection);
    }
}

删除

package com.NBUFE.JDBC;

import com.NBUFE.util.DBUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TextjdbcDelete { 
   
    public static void main(String[] args){ 
   
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        DBUtil dbUtil=null;
        try { 
   
            dbUtil=new DBUtil();
            connection = dbUtil.getConnection();
            String sql = "delete from student where id = ? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,2);
            preparedStatement.executeUpdate();
        }
        catch (Exception e){ 
   
            e.printStackTrace();
        }
        finally { 
   
            dbUtil.closeAll(null,preparedStatement,connection);
        }
    }
}

对File进行操作

  1. 创建文件aaa.txt
  2. 重命名bbb.txt
  3. 删除bbb.txt
  4. 在a文件下有 ccc.txt ddd.txt eee.txt 通过java代码把a文件夹下的文件名打印出来
  5. 在aaa.txt hello 我好开心,作业很少

代码

package com.NBUFE.text;

import java.io.*;

public class TextFile { 
   
    public static void main(String[] qrgs) throws IOException { 
   
        //新建文件
        //File file = new File("C:\\Hello\\aaa.text");
        //file.createNewFile();
        //System.out.println(file);
        //重命名文件
        //File file1 = new File("C:\\Hello\\bbb.text");
        //file.renameTo(file1);
        //删除文件
        //File file2 = new File("C:\\Hello\\aaa.text");
        //file2.delete();
        //输出文件中的文件名
        //File file3 = new File("C:\\Hello");
        //String[] list = file3.list();
        //for(String s:list){ 
   
        // System.out.println(s);
        //}
        //在文件中写入内容
        //FileWriter file4 = new FileWriter("C:\\Hello\\1.text");
        //file4.write("hello 我好高兴 作业很少");
        //file4.close();
        //在文件中读出内容
        FileReader file5 = new FileReader("C:\\Hello\\1.text");
        //读出内容长度
        //int read = file5.read;
        BufferedReader file6 = new BufferedReader(file5);
        String s = file6.readLine();
        System.out.println(s);
    }
}

今天的文章1-13JAVA课程学习分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26758.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注