html/css的网页制作

来源:互联网 发布:淘宝一键生成手机详情 编辑:程序博客网 时间:2024/04/29 23:27

在上一周我们学习了mysql数据库的连接和对数据库在Java中进行增删查改,做了一个简单的书籍管理系统,不是很完美但是基本功能还是实现了。

下面看看我的数据库连接;

private String driver = "com.mysql.jdbc.Driver";// 数据库驱动
private String ur1 = "jdbc:mysql://localhost:3306/bookmessage";// 数据连接
private String user = "root";// 数据库用户名
private String password = "root";
private Connection conn = null;// 数据连接对象


//
public Connection getConnection() {
if (conn == null) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(ur1, user, password);
} catch (Exception e) {
e.printStackTrace();// 异常处理
}
}
return conn;// 返回连接对象
}


// 关闭连接
public void closeALL(Connection conn, Statement stmt, ResultSet rs) {
//
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();// 异常处理
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();// 异常处理
}

下面是对书籍操作人员的的增删查改;

       PreparedStatement psmt = null;
Datedao dda = new Datedao();//引用数据库
Connection conn = dda.getConnection();
ResultSet rs = null;

// 录入一条数据
@Override
public boolean add(int operatorId, String logName, String passWord,
String operatorName, String operatorSex, String cellthoneNumber,
String operatorState) {
// TODO Auto-generated method stub
boolean f = false;
int a = 0;
String sql = "insert into operator(operatorId,logName, passWord,operatorName,operatorSex,cellphoneNumber,operatorState)values(?,?,?,?,?,?,?)";
try {
psmt = conn.prepareStatement(sql);
psmt.setInt(1, operatorId);
psmt.setString(2, logName);
psmt.setString(3, passWord);
psmt.setString(4, operatorName);
psmt.setString(5, operatorSex);
psmt.setString(6, cellthoneNumber);
psmt.setString(7, operatorState);
a = psmt.executeUpdate();
if (a > 0) {
f = true;
} else {
f = false;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
// psmt.close();
// conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return false;
}


// 删除一个用户
@Override
public int del(int operatorId) {
String sql = "delete from operator where operatorId ='" + operatorId
+ "'";
try {
Statement statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}


// 修改一个用户密码
public int update(int operatorId, String passWord) {
// TODO Auto-generated method stub
String sql = "update operator set passWord='" + passWord
+ "' where operatorId =" + operatorId;
int change = 0;
try {
// Statement statement;
psmt = conn.prepareStatement(sql);
change = psmt.executeUpdate(sql);
if (change > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}


// 查询一个用户实现登陆功能
@Override
public boolean findByName(int operatorId, String logName, String passWord) {
// TODO Auto-generated method stub
boolean f = false;
String sql = "select * from operator where operatorId=? and logName=? and passWord=? ";
try {
psmt = conn.prepareStatement(sql);
psmt.setInt(1, operatorId);
psmt.setString(2, logName);
psmt.setString(3, passWord);
rs = psmt.executeQuery();
if (rs.next()) {
f = true;
} else {
f = false;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return f;
}

1 0
原创粉丝点击