File 的文件改名
import java.io.IOException;
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<list.length;i++)
{
System.out.println(list[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);
}
}
增删改查
package zhongruan;
import zhongruan.utlie.DBUtil;
import zhongruan.utlie.TestCreat;
import zhongruan.utlie.TestJdb;
import zhongruan.utlie.TestUpade;
import javax.security.auth.login.LoginException;
import java.sql.*;
import java.lang.String;
import java.util.Scanner;
public class Longin {
public static void main(String[] args){
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/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
//sql语句
String sql = "select * from student where id=? and name=? and password=? ";
System.out.println("请输入用户名:");
String name=input.next();
System.out.println("请输入密码:");
String password=input.next();
//得到statement对象
statement = connection.prepareStatement(sql);
statement.setString(1,name);
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:
TestCreat.add();break;
case 2:
TestDelect.delect();break;
case 3:
TestUpade.upade();break;
case 4:TestJdb.select();break;
}
System.out.println("退出请按0");
if(input.nextInt()==0)
{
break;
}
}
}
else{
throw new LoginException("用户名密码不对");
}
}
catch (SQLException | LoginException e){
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet,statement,connection);
}
System.out.println("111111");
}
}
package zhongruan.utlie;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestSelect {
public static void main(String[] args) throws Exception {
//加载驱动/
Connection connection=DBUtil.getConnection();
PreparedStatement preparedStatement=null;
try {
String sql = "select * from student where id=1";
//获得statement对象
preparedStatement= connection.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery(sql);
if(rs.next())
{
int m1 = rs.getInt(1);//或者为rs.getString(1),根据数据库中列的值类型确定,参数为第一列
String m2 = rs.getString(2);
System.out.println(m2);
}
//preparedStatement.setString(2, "sad");
//preparedStatement.setInt(3, 34);
//preparedStatement.executeUpdate();
}
catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
}
}
package zhongruan.utlie;
import zhongruan.bean.Student;
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 TestJdb {
public static void select() throws Exception {
ResultSet resultSet=null;
PreparedStatement preparedStatement=null;
Connection connection = DBUtil.getConnection();
List<Student> students=new ArrayList<>();
//3.写sql语句
try {
String sql="select * from student";
//4.得到statement对象
preparedStatement = connection.prepareStatement(sql);
//5.执行sql得到结果集
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);
}catch (SQLException e)
{
e.printStackTrace();
}
finally {
//7.关闭资源
DBUtil.closeAll(resultSet,preparedStatement,connection);
};
}
}
package zhongruan.utlie;
import javax.swing.plaf.synth.SynthSpinnerUI;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class TestUpade {
public static void upade() {
//加载驱动/
Scanner input = new Scanner(System.in);
Connection connection = DBUtil.getConnection();
int resultSet = 0;
PreparedStatement preparedStatement = null;
while (true) {
System.out.println(“修改请按1,退出请按0”);
int t;
t = input.nextInt();
if (t == 0) {
break;
}
try {
String sql = "update student set password =? where name =?";
//获得statement对象
preparedStatement = connection.prepareStatement(sql);
//preparedStatement.setString(1, input.next());
System.out.println("请输入用户名");
String b = input.next();
System.out.println("修改的密码");
String c = input.next();
preparedStatement.setString(1, c);
preparedStatement.setString(2, b);
resultSet=preparedStatement.executeUpdate();
//preparedStatement.setString(2, "sad");
//preparedStatement.setInt(3, 34);
//preparedStatement.executeUpdate();
if(resultSet!=0)
{
System.out.println("修改成功");
}
else
System.out.println("修改失败");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(null, preparedStatement, connection);
}
}
}
}