properties配置文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/epet
user=root
password=1234
读取配置文件:
public class BaseDao {
private static String driver;//数据库驱动字符串
private static String url;//连接URL字符串
private static String user;//数据库用户名
private static String password;//数据库用户密码
/*
* driver=com.mysql.jdbc.Driver
* url=jdbc:mysql://localhost:3306/epet
* user=root
* password=1234
*/
static {//静态代码块,在类加载的时候执行
init();
}
public static void init() {
try {
Properties pro = new Properties();
InputStream is = BaseDao.class.getClassLoader()
.getResourceAsStream("database.properties");//加载配置文件到输入流中,配置文件路径
//从输入流中读取属性列表
pro.load(is);
//根据指定的获取对应的值
driver = pro.getProperty("driver");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
实体类
1.实体类的属性一般使用 private 修饰。
2根据业务需要和封装性要求对实体类的属性提供 getter/setter 方法,负责属性的读取和赋值,一般使用 public 修饰。
3.对实体类提供无参构造方法,根据业务需要提供相应的有参构造方法。
4.实体类最好实现 java.io.Serializable 接口,支持序列化机制,可以将该对象转换成字节序列而保存在磁盘或在网络上传输。
5.如果实体类实现了 java.io.Serializable 接口,就应该定义属性 serialVersionUID,解决不同版本之间的序列化问题。
public class Epet implements Serializable{