准备材料
mysql数据库+接口的jar包(根据自己数据库版本去官网下载 ------ 例:mysql-connector-java-5.1.48.jar)Eclipse开发工具+基本编译运行环境
具体步骤
注册驱动创建连接准备sql语句准备sql执行模板执行sql语句关闭连接
注册驱动 创建连接 1.将自己下载的jar包导入Build Path 路径—单击工程右键属性找到 Build Path\Configure Build Path\Add External JARs 按路径导入即可 2.如下代码是固定套路,因为经常调用,可以在一个方法中具体生成以便减少代码
1. Class.forName("com.mysql.jdbc.Driver");
2. Connection coon=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydata",root,password);
第二条语句中的三个参数分别是 URL,数据库用户名,密码;自己酌情添加
准备sql语句 和执行模板 执行语句 关闭连接
String sql="select * from student where book_name =?";
PreparedStatement ps=coon.prepareStatement(sql);
ps.setInt(name);
ps.executeUpdate()
ps.close();
coon.close();
以下是运行截图
具体功能代码
增添图书 public static void add(String uni,String name) throws Exception{ connection(); String sql=“insert into books values(?,?,1,0,null)”; ps = con.prepareStatement(sql); ps.setString(1,uni); ps.setString(2,name); ps.executeUpdate(); System.out.println(“添加成功”); ps.close(); con.close(); }
//创建驱动 public static void connection() throws Exception { Class.forName(“com.mysql.jdbc.Driver”); con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydata”, “root”, “myroot”); }
//靠书名查询 public static void select(String x) throws Exception{ connection(); String sql=“select * from books where name =?”; ps=con.prepareStatement(sql); ps.setString(1, x); ResultSet rs = ps.executeQuery(); System.out.println(“编号\t书名\t状态(库存)\t借书次数\t最新借出日期”); String uni=""; while(rs.next()){ uni=rs.getString(“uni”); String name=rs.getString(“name”); int state=rs.getInt(“state”); int times=rs.getInt(“lendtimes”); String date=rs.getString(“date”); System.out.println(uni+"\t"+name+"\t"+state+"\t"+times+"\t"+date); } if(uni=="") System.out.println(“未找到该图书!”); rs.close(); ps.close(); con.close(); }
借书 public static void borrow(String u,String y) throws Exception{ connection(); int i = -1; String sql=“select * from books where name=?”; ps=con.prepareStatement(sql); ps.setString(1, u); ResultSet rs = ps.executeQuery(); while(rs.next()){ i=rs.getInt(“state”); } if(i==-1) System.out.println(“暂无此图书”); else if (i>0){ String sq=“update books set state =state-1 ,lendtimes =lendtimes +1,date = ? where name =?”; PreparedStatement pt = con.prepareStatement(sq); pt.setString(2, u); pt.setString(1, y); pt.executeUpdate(); System.out.println(“已成功借出”); } else if(i==0)System.out.println(“暂无库存”); }
还书 public static void back(String u) throws Exception{ connection(); int i=-1; String sql=“select * from books where uni=?”; ps=con.prepareStatement(sql); ps.setString(1, u); ResultSet rs = ps.executeQuery(); while(rs.next()){ i=rs.getInt(“state”); } if(i==-1) System.out.println(“书库中暂无需要归还该书记录”); else if(i>=0){ String sq=“update books set state =state+1 where uni =?”; PreparedStatement pt = con.prepareStatement(sq); pt.setString(1, u); pt.executeUpdate(); System.out.println(“归还成功”); } }
//图书更新 public static void update(String uni,String name,int state) throws Exception{ connection(); String sql=“update books set name = ?,state = ? where uni = ?”; ps=con.prepareStatement(sql); ps.setString(3, uni); ps.setInt(2, state); ps.setString(1, name); if(ps.executeUpdate()!=0) //ps.executeUpdate(); System.out.println(“成功更新”); else System.out.println(“更新失败”); ps.close(); con.close(); } 查看库存和图书下架跟上边代码一个模子稍作修改就行,对于稍微知道sql和java的同学来说,我在写下去就相当与太侮辱智商了。