file的增加新文件和改名
代码如下:
package com.nbufe.test;
import java.io.File;
import java.io.IOException;
public class Testfile {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\kaiinn father\\Desktop\\1.txt");
file.createNewFile();
File file1=new File("C:\\Users\\kaiinn father\\Desktop\\2.txt");
file.renameTo(file1);
file1.delete();
System.out.println(file);
}
}
然后就是如何用file来读取一个文件夹下的所有文件名
代码如下:
package com.nbufe.test;
import java.io.File;
public class Testfile2 {
public static void main(String[] args) {
File file=new File("C:\\Users\\kaiinn father\\Desktop");
String[] list=file.list();
for(int i=0;i
用file来建立新的文件并且往文件中写入文字
代码如下:
package com.nbufe.test;
import java.io.*;
public class Testfile3 {
public static void main(String[] args) throws IOException {
File file=new File("D:\\TEST\\132.jpg");
file.createNewFile();
FileWriter fileWriter=new FileWriter("C:\\Users\\kaiinn father\\Desktop\\1.txt");
fileWriter.write("hh 作业好少真开心");
fileWriter.close();
FileReader fileReader=new FileReader("C:\\Users\\kaiinn father\\Desktop\\1.txt");
BufferedReader bufferedReader=new BufferedReader(fileReader);
String x=bufferedReader.readLine();
System.out.println(x);
}
}
在最后我们完善了jdbc的增删改查
代码如下:
package com.nbufe.test;
public class TestFinallyjdbc {
public static void main(String[] args) {
try {
TestLogin.Login();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.nbufe.test;
import java.sql.*;
import java.util.Scanner;
public class TestLogin {
public static void Login() throws Exception {
//1.加载驱动
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
Scanner input=new Scanner(System.in);
try {
Class.forName("com.mysql.jdbc.Driver") ;
}catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try {
connection= DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=123");
String sql="select * from user where username=? and password=?";
statement=connection.prepareStatement(sql);
String username,password;
System.out.println("请输入用户名");
username=input.next();
System.out.println("请输入密码");
password=input.next();
statement.setString(1,username);
statement.setString( 2,password);
resultSet=statement.executeQuery();
if (resultSet.next())
{
System.out.println("登陆成功");
while (true)
{
System.out.println("请选择你需要的功能1增加2删除3修改4查询");
switch (input.nextInt())
{
case 1:Testadd.add();break;
case 2:Testdelect.delect();break;
case 3:Testupdate.update();break;
case 4:Testselect.select();break;
}
System.out.println("退出请按0");
if(input.nextInt()==0)
{
break;
} }
}
else {
throw new LoginException("用户名密码错误");
}
}catch (SQLException e)
{
e.printStackTrace();
}finally {
if(resultSet!=null)
{
try {
resultSet.close();
}catch (SQLException e)
{
e.printStackTrace();
}
}
if (statement!=null) {
try {
statement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection!=null)
{
try {
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
package com.nbufe.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Testadd {
public static void add() {
while (true)
{
Scanner input=new Scanner(System.in);
System.out.println("添加请按1退出请按0");
int a;
a=input.nextInt();
if(a==0)
{
break;
}
PreparedStatement preparedStatement=null;
int resultSet=0;
Connection connection=DBUtil.getconnection();
//3.写sql语句
try {
String sql="insert into user (username,password)value (?,?)";
//4.得到statement对象
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,input.next());
preparedStatement.setString(2,input.next());
//5.执行sql语句得到结果集
resultSet=preparedStatement.executeUpdate();
if(resultSet!=0)
{
System.out.println("增加成功");
}
else
System.out.println("增加失败");
//6.处理结果集
}catch (SQLException e)
{
e.printStackTrace();
}
finally {
DBUtil.close(null,preparedStatement,connection);
}
}
}
}
package com.nbufe.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Testdelect {
public static void delect() {
while (true)
{
Scanner input=new Scanner(System.in);
System.out.println("删除请按1退出请按0");
int a;
a=input.nextInt();
if(a==0)
{
break;
}
PreparedStatement preparedStatement=null;
int resultSet=0;
Connection connection=DBUtil.getconnection();
//3.写sql语句
try {
String sql="delete from user where username=?";
//4.得到statement对象
preparedStatement=connection.prepareStatement(sql);
String x=input.next();
preparedStatement.setString(1,x);
//5.执行sql语句得到结果集
resultSet=preparedStatement.executeUpdate();
if(resultSet!=0)
{
System.out.println("删除成功");
}
else
System.out.println("删除失败");
//6.处理结果集
}catch (SQLException e)
{
e.printStackTrace();
}
finally {
DBUtil.close(null,preparedStatement,connection);
}
}
}
}
package com.nbufe.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Testupdate {
public static void update() {
PreparedStatement preparedStatement=null;
Scanner input=new Scanner(System.in);
int resultSet=0;
Connection connection=DBUtil.getconnection();
//3.写sql语句
while (true)
{
System.out.println("修改请按1,退出请按0");
int t;
t=input.nextInt();
if(t==0)
{
break;
}
try {
String sql="update user set password=? where username=? ";
//4.得到statement对象
preparedStatement=connection.prepareStatement(sql);
System.out.println("请输入用户名");
String b=input.next();
System.out.println("修改的密码");
String c=input.next();
preparedStatement.setString(1,c);
preparedStatement.setString(2,b);
//5.执行sql语句得到结果集
resultSet=preparedStatement.executeUpdate();
if(resultSet!=0)
{
System.out.println("修改成功");
}
else
System.out.println("修改失败");
//6.处理结果集
}catch (SQLException e)
{
e.printStackTrace();
}
finally {
DBUtil.close(null,preparedStatement,connection);
}
}
}
}
package com.nbufe.test;
import com.nbufe.HomeWork.bean.Student1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Testselect {
public static void select() throws Exception {
//1.加载驱动
List students=new ArrayList<>();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
Connection connection=DBUtil.getconnection();
//3.写sql语句
try {
String sql="select * FROM user ";
//4.得到statement对象
preparedStatement=connection.prepareStatement(sql);
//5.执行sql语句得到结果集
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
Student1 student=new Student1();
student.setId(resultSet.getInt(1));
student.setUsername(resultSet.getString(2));
student.setPassword(resultSet.getString(3));
students.add(student);
}
System.out.println(students);
//6.处理结果集
}catch (SQLException e)
{
e.printStackTrace();
}
finally {
DBUtil.close(resultSet,preparedStatement,connection);
}
//7.关闭资源
}
}