📄 admindao.java
字号:
package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class AdminDao {
// 取得一个数据库连接
public Connection getConnection()throws SQLException,InstantiationException,IllegalAccessException,
ClassNotFoundException{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
// String url = "jdbc.mysql://192.168.2.164:3306/shopping";
// String user = "D6";
// String password = "123";
String url = "jdbc:mysql://localhost/shopping?user=admin&password=admin";
conn = DriverManager.getConnection(url);
return conn;
}
// 根据传入的SQL语句,返回一个对象的链表
public ArrayList adminSelect(String sql)throws Exception{
ArrayList<Admin> result = new ArrayList<Admin>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
Admin admin = new Admin();
admin.setAdminname(rs.getString("adminname"));
admin.setPassword(rs.getString("password"));
result.add(admin);
}
}catch (SQLException sqle){
throw new SQLException("select data exception: "+ sqle.getMessage());
}catch(Exception e){
throw new Exception("System e exception: "+e.getMessage());
}finally{
try{
if(rs != null){
rs.close();
}
}catch(Exception e){
throw new Exception("statement close exception: "+ e.getMessage());
}
try{
if(stmt !=null){
stmt.close();
}
}catch (Exception e){
throw new Exception("statement close exception: "+e.getMessage());
}
try{
if(conn != null){
conn.close();
}
}catch(Exception e){
throw new Exception("cennection close exception: "+ e.getMessage());
}
}
return result;
}
// 根据传入的对象向数据库插入一条记录
public void adminInsert(Admin admin) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
// 根据对象的属性生成SQL语句
String sql = "insert into admin(adminname,password)values('"+ admin.getAdminname()+"','"+admin.getPassword()+"')";
try{
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
}catch(SQLException sqle){
throw new Exception ("insert data exception "+ sqle.getMessage());
}finally{
try{
if(ps != null){
ps.close();
}
}catch(Exception e){
throw new Exception("statement close exception: "+ e.getMessage());
}
try{
if(conn != null){
conn.close();
}
}catch(Exception e){
throw new Exception("cennection close exception: "+ e.getMessage());
}
}
}
// 根据传入的对象更新数据库记录
public void adminUpdate(Admin admin) throws Exception{
Connection conn = null;
PreparedStatement ps =null;
// 根据对象的属性生成SQL语句
String sql = "update admin set password='"+admin.getPassword()+"'where adminname='"+admin.getAdminname()+"'";
try{
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
}catch(SQLException sqle){
throw new Exception ("update data exception "+ sqle.getMessage());
}finally{
try{
if(ps != null){
ps.close();
}
}catch(Exception e){
throw new Exception("statement close exception: "+ e.getMessage());
}
try{
if(conn != null){
conn.close();
}
}catch(Exception e){
throw new Exception("cennection close exception: "+ e.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -