chapter6.zip

  • ohhh12
    了解作者
  • Java
    开发工具
  • 69KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-11-29 21:32
    上传日期
jdbc 连接mysql以及jdbc的简单使用,可以帮助初学者快速入门
chapter6.zip
内容介绍
package com.my.bean; import java.io.UnsupportedEncodingException; import java.sql.*; public class BookManage { private String bookname = "aa",author = "aaa",publish = "aaa"; private float price = 34.5f; private StringBuffer books = null; private boolean flag; public String changeStr(String str){ try { str = new String(str.getBytes("iso-8859-1"),"utf-8"); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } return str; } public void setBookname(String bookname){ this.bookname = changeStr(bookname); } public void setAuthor(String author) { this.author = changeStr(author); } public void setPublish(String publish) { this.publish = changeStr(publish); } public String getBookname() { return bookname; } public StringBuffer getBooks(){ Connection conn = null; Statement stmt = null; try{ conn = DbUtil.getConnection(); stmt = conn.createStatement(); //String sql = "select * from book"; //String sql = "select * from book where bookname like '%" + bookname +"%'"; //ResultSet rs = stmt.executeQuery(sql);* // String sql = "select * from book where bookname like ?"; // PreparedStatement ps = conn.prepareStatement(sql); // ps.setString(1, "%" + "a" + "%"); // ResultSet rs = ps.executeQuery(); books = new StringBuffer(); String sql = "select * from book"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ books.append("<tr>"); books.append("<td>" +rs.getInt("BookId")+"</td>"); books.append("<td>" +rs.getString("BookName")+"</td>"); books.append("<td>" +rs.getString(3)+"</td>"); books.append("<td>" +rs.getString(4)+"</td>"); books.append("<td>" +rs.getFloat(5)+"</td>"); books.append("</tr>"); } } catch(SQLException e){ e.printStackTrace(); } finally{ DbUtil.close(conn,stmt); } return books; } public String getInsertBook(){ String result = ""; Connection conn = null; Statement stmt = null; flag = false; try{ conn = DbUtil.getConnection(); stmt = conn.createStatement(); /*String sql = "insert into book(bookName,author,publish,price)" +"values('"+bookname+"','"+author +"','+publish+"',"+price+")"; int n = stmt.executeUpdate(sql); */ String sql = "select * from book where bookname='" + bookname + "'"; ResultSet rs = stmt.executeQuery(sql); if(!rs.next()) { sql = "insert into book(bookName,author,publish,price)"+"values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, bookname); ps.setString(2, author); ps.setString(3, publish); ps.setFloat(4, price); int n = ps.executeUpdate(); if(n > 0) { flag = true; result = "插入成功!"; } else { result = "插入失败,操作数据库失败!"; } } else { result = "插入失败,本书已存在!"; } } catch(SQLException e) { result = "数据库操作异常!"; e.printStackTrace(); } finally{ DbUtil.close(conn,stmt); } return result; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } }
评论
    相关推荐