201621123061《Java课程设计》第14次学习总结
1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容。
2. 使用数据库技术改造你的系统
2.1 简述如何使用数据库技术改造你的系统。要建立什么表?截图你的表设计。
系统所需要的信息都存储在数据库中,例如图书信息、读者信息、借阅信息等,要对这些信息进行操作,就必须连接数据库,为了省去每次操作都要编写连接数据库程序,我们把连接数据库操作封装到一个类jdbcfile.java中,在不同的模块中调用这个类就可以对数据库进行连接,执行相应的数据库操作 表:图书信息表、读者信息表、借阅信息表等。
2.2 系统中使用到了JDBC中什么关键类?
DriverManager、Connection、Statement、resultSet
2.3 截图数据库相关模块的关键代码。关键行需要加注释。
import java.sql.*;
public class jdbcfile {
Connection conn;
Statement stmt;
int inorupdatevalue=-1;
//声明构造方法,并抛出异常
public jdbcfile() throws Exception {
try{
String drivername="jdbc.MySQLDriver";
String dbURL = "jdbc:mysql://localhost:1489/test";
//加载驱动程序
Class.forName(drivername);
//创建数据库连接Connection对象
conn=DriverManager.getConnection(dbURL, "chenjinxia", "chenjinxia123");
//创建Statement对象
stmt=conn.createStatement();
}catch(ClassNotFoundException e){
//捕获异常
throw new Exception("数据库驱动未找到"+e.getMessage());
}catch(SQLException e){
//捕获异常
throw new Exception("数据库未连接"+e.getMessage());
}
}
//定义查询数据的方法
public synchronized ResultSet executeQuery(String sql) throws Exception{
ResultSet rs=stmt.executeQuery(sql);
return rs;
}
//定义插入数据的方法
public synchronized int insert(String sql) throws Exception {
inorupdatevalue=stmt.executeUpdate(sql);
return inorupdatevalue;
}
//定义修改数据的方法
public synchronized int update(String sql) throws Exception {
inorupdatevalue=stmt.executeUpdate(sql);
return inorupdatevalue;
}
//定义删除数据的方法
public synchronized int del(String sql) throws Exception {
inorupdatevalue=stmt.executeUpdate(sql);
return inorupdatevalue;
}
//定义关闭数据库连接的方法
public void close() throws Exception{
conn.close();
}
}
//创建数据库连接
jdbcfile conn=new jdbcfile();
//产生登录sql语句
sqlStr="select * from Admin where num='"+str1+"'and password='"+str2+"'";
ResultSet result=conn.executeQuery(sqlStr);
if(result.next()){
//弹出对话框提示登录成功
JOptionPane.showMessageDialog(frame,"登录成功!");
//打开图书管理主页面
bookmain bookmain1=new bookmain();
bookmain1.go();
//关闭登录窗口
frame.dispose();
//关闭数据库连接
conn.close();
}else{
JOptionPane.showMessageDialog(frame,"用户名或密码错误");
}
}else if(obj.equals(Cancel_btn)){
//点击取消按钮
System.exit(0);
}
}catch(ClassNotFoundException ce){
//捕获异常
System.out.println("SQLException:"+ce.getMessage());
}
catch(SQLException ex){
//捕获异常
System.out.println(ex);
}
catch (Exception s) {
//捕获异常
s.printStackTrace();
}
}
}
2. 书面作业
1. MySQL数据库基本操作
建立数据库,将自己的姓名、学号作为一条记录插入。(截图,需出现自己的学号、姓名)
在自己建立的数据库上执行常见SQL语句(截图)