ackage com.nbufe.test;
public class Test2
{
public static void main(String[] args) {
Person person=new Person();
try {
person.setAge(-1);
}catch (AgeException e)
{
e.printStackTrace();
}
int age=person.getAge();
System.out.println(age);
}
}
package com.nbufe.test;
public class Person {
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age<0||age>=150)
{
throw new AgeException("你可能不是人");
}
else
this.age = age;
}
private int age;
}
package com.nbufe.test;
public class AgeException extends Exception{
public AgeException(String message) {
super(message);
}
}
package com.nbufe.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestLogin {
public static void main(String[] args) throws Exception {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
Connection connection= DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=chestnut&password=321");
System.out.println("创建连接成功");
String sql="select * from user where username =1 and password =781376680" ;
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//5.执行sql语句得到结果集
ResultSet resultSet=preparedStatement.executeQuery();
//6.处理结果集
if (resultSet.next())
{
System.out.println("11111");
}
else
{
throw new LoginException("登陆错误");
}
}
}
package com.nbufe.test;
public class LoginException extends Exception {
public LoginException(String message) {
super(message);
}
}