Java操作数据库新增数据的大概流程:
1)创建数据库连接(这里用的是MySQL数据库)
private static String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test"; //数据源(mysql)的连接地址
String user = "root"; //用户名
String password = "root"; //用户密码
static{
try {
//加载驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
//关闭资源
public static void close(Connection con,PreparedStatement ps,ResultSet rs){
try {
if(con !=null){
con.close();
}
if(ps !=null){
ps.close();
}
if(rs !=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2)在页面获取数据