1.登录异常
import sun.rmi.transport.Connection;
import javax.security.auth.login.LoginException;
import java.io.RandomAccessFile;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Denglu {
public Denglu(String message) {
}
public static void main(String[] args)throws LoginException{
Connection connection=null;
PreparedStatement statement=null;
Scanner input=new Scanner(System.in);
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
//2.创建连接
try {
connection = (Connection) DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
String sql = "select * from student where username=? and password=? ";
System.out.println("请输入用户名");
String name=input.next();
System.out.println("请输入密码:");
String password=input.next();
//4.得到statement对象
statement = connection.prepareStatement(sql);
statement.setString(1, "123");
statement.setString(2, "123");
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()){
System.out.println("登入成功");
}else {
throw new LoginException("用户名密码不对");
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
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();
}
}
System.out.println("111111");
//5.执行sql得到结果集
//6.处理结果集
//7.关闭资源
} catch (SQLException e) {
e.printStackTrace();
}
}
2.file操作
import com.sun.java.util.jar.pack.Package;
import java.io.File;
import java.io.IOException;
public class Testfile {
public static void main(String[] args)throws IOException {
File file=new File("D:\\PPT模板\\aaa.txt");
file.createNewFile();
File file1=new File("D:\\PPT模板\\bbb.txt");
file.renameTo(file1);
System.out.println(file);
}
}