前端时间帮朋友做了一个农村人口管理系统,也算是对自己的Java的锻炼吧。不过我只做了对户头的增删改查,其他操作也类似于户头这样。
这个系统要实现的功能如下:
本次系统使用的是MySql数据库,数据驱动包是:
如果你是8点几版本的,可以自行去查找一下相关配置,毕竟两者还是稍微有点差别,如果没弄好可能无法连接到数据库。
接下来是数据库的设计:主要是设计了4个表,实际上应该还有其他的表但是我懒,不想写了。
以上就是这个系统的环境搭建,数据库和环境一定要搭好很多问题就是出在这上面。
下面就是连接数据库的方法:
附上关键代码:这是jdbc.propertice的内容.
driver=com.mysql.jdbc.Driver
user=root
password=你自己的数据库密码
url=jdbc:mysql://localhost:3306/你自己的数据库名字?useUnicode=true&characterEncoding=utf8
如果你不想写也可以把这个内容换到下面的代码所对应的内容中去:
public class myDataBase {
private static String url;//数据库地址
private static String driver;
private static String user;
private static String password;//数据库登录密码
static {
//读取配置文件
Properties properties = new Properties();
FileInputStream file = null;
try {
file = new FileInputStream("D:\\java\\农村人口统计系统\\src\\jdbc.properties");
//调用方法,加载文件file
properties.load(file);
//读取文件内容
url = properties.getProperty("url");
driver = properties.getProperty("driver");
user = properties.getProperty("user");
password = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//获取Connection与数据库建立联系
public static Connection getConnection() {
Connection connection = null;
try {
//搜索并加载相应的driver
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//关闭数据流
public static void close(PreparedStatement ps,Connection connection){
if (ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}