jdbc登录
public class Longin {
public static void main(String[] args) throws LoginException{
Connection connection=null;
PreparedStatement statement=null;
Scanner input = new Scanner(System.in);
ResultSet resultSet=null;
//1.加载驱动
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.创建连接
try {
connection= DriverManager.getConnection
(“jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123”);
//3.写sql
String sql=“select * from student where name=? 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,name);
statement.setString(2,password);
resultSet = statement.executeQuery();
if(resultSet.next()){
System.out.println(“登录成功”);
}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();
}
}
}
System.out.println(“创建连接成功”);
}
}