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);
}
}
}