国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

JDBC案例演示,供參考

2019-11-11 07:49:36
字體:
供稿:網(wǎng)友

在這寫一下JDBC的案例,將JDBC的一些功能演示下,供參考,至于實體類在此我就不寫出來了,大家根據(jù)需求不同記得導(dǎo)入對應(yīng)的驅(qū)動包到項目中,我這里演示的是以MySQL為例,其實區(qū)別都不大,如有不對的地方歡迎糾正。

import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import com.mysql.jdbc.PReparedStatement;import java.sql.ResultSet;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Clob;import java.sql.DatabaseMetaData;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Savepoint;public class JDBCDemo { // 數(shù)據(jù)庫地址 localhost表示本地, //3306是mysql默認(rèn)的端口,db_bank是要鏈接的數(shù)據(jù)庫 private static String dbUrl="jdbc:mysql://localhost:3306/db_bank"; // 用戶名 private static String dbUserName="root"; // 密碼 private static String dbPassWord="123456"; // 驅(qū)動名稱 這里是mysql驅(qū)動 private static String jdbcName="com.mysql.jdbc.Driver"; //靜態(tài)塊,最優(yōu)先執(zhí)行 static{ try{ Class.forName(jdbcName);//加載驅(qū)動 }catch(Exception e){ e.printStackTrace();//打印錯誤信息 } } /** * 獲取數(shù)據(jù)庫連接 * @return * @throws Exception */ public static Connection getCon()throws Exception{ //獲取數(shù)據(jù)庫鏈接 Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword); return con; } /** * 關(guān)閉數(shù)據(jù)庫連接 * @param con * @param sta * @param rs * @throws Exception */ public static void close(Connection conn,Statement sta,ResultSet rs){ try{ if(rs!=null){ rs.close();//關(guān)閉ResultSet } if(sta!=null){ sta.close();//關(guān)閉Statement } if(conn!=null){ conn.close();//關(guān)閉Connection } }catch(Exception e){ e.printStackTrace();//打印異常信息 } } /** * 新增演示[添加圖書] * @param book 要新增的圖書對象 * @return 返回受影響的行數(shù) * @throws Exception */ private int addBook(Book book)throws Exception{ Connection con=getCon();//得到數(shù)據(jù)庫鏈接 //得到PreparedStatement對象 PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, book.getBookName()); // 設(shè)置圖書名稱 pstmt.setFloat(2, book.getPrice()); // 設(shè)置圖書價格 pstmt.setString(3, book.getAuthor()); // 設(shè)置圖書作者 pstmt.setInt(4, book.getBookTypeId()); // 設(shè)置圖書類型Id //--------- 大數(shù)據(jù)字符集 -------------- InputStream inputStream=new FileInputStream(new File("c:/hello.text"));//得到輸入流 //大數(shù)據(jù)類型,設(shè)置圖書內(nèi)容, //這里演示mysql數(shù)據(jù)庫類型是longtext pstmt.setAsciiStream(5,inputStream,context.length()); //----大數(shù)據(jù)二進(jìn)制,一般存儲圖片,視頻,音頻等 ---- //將圖片轉(zhuǎn)為輸入流 InputStream inputStream2=new FileInputStream(new File("c:/a.jpg")); //大數(shù)據(jù)類型,設(shè)置封面圖片, //這里演示mysql數(shù)據(jù)庫類型是longblod pstmt.setBinaryStream(6, inputStream2, pic.length()); //執(zhí)行并返回受影響的行數(shù) int result=pstmt.executeUpdate(); if(result>0){//大于0表示新增成功 //獲取生成器 ResultSet rs=pstmt.getGeneratedKeys(); if(rs.next()){//判斷是否有值 //得到新增數(shù)據(jù)后的主鍵值 int key=rs.getInt(1); } } close(con,pstmt,null);//關(guān)閉連接 return result; } /** * 修改演示[更新圖書] * @param book 要修改的圖書對象 * @return 返回受影響的行數(shù) * @throws Exception */ private int updateBook(Book book)throws Exception{ Connection con=getCon();//得到數(shù)據(jù)庫鏈接 //新增語句 String sql="update t_book set bookName=?,price=?,author=?,bookTypeId=? where id=?"; //得到PreparedStatement對象 PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, book.getBookName());//設(shè)置圖書名稱 pstmt.setFloat(2, book.getPrice());//設(shè)置圖書價格 pstmt.setString(3, book.getAuthor());//設(shè)置圖書作者 pstmt.setInt(4, book.getBookTypeId());//設(shè)置圖書類型Id pstmt.setInt(5, book.getId());//設(shè)置要修改的圖書id //執(zhí)行修改并返回受影響的行數(shù) int result=pstmt.executeUpdate(); close(con,pstmt,null);//關(guān)閉連接 return result; } /** * 刪除演示[刪除圖書] * @param id 要刪除的圖書id * @return 返回受影響的行數(shù) * @throws Exception */ private int deleteBook(int id)throws Exception{ Connection con=getCon();//得到數(shù)據(jù)庫鏈接 String sql="delete from t_book where id=?";//刪除語句 //得到PreparedStatement對象 PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setInt(1, id);//設(shè)置要刪除的圖書id //執(zhí)行刪除并返回受影響的行數(shù) int result=pstmt.executeUpdate(); close(con,pstmt,null);//關(guān)閉連接 return result; } /** * 查詢演示[查詢所有圖書] * @return 返回list集合 * @throws Exception */ private List<Book> listBook()throws Exception{ List<Book> bookList=new ArrayList<Book>(); Connection con = getCon(); //得到數(shù)據(jù)庫鏈接 String sql = "select * from t_book";//查詢語句 //得到PreparedStatement對象 PreparedStatement pstmt = con.prepareStatement(sql); //執(zhí)行并返回結(jié)果集ResultSet ResultSet rs = pstmt.executeQuery(); while (rs.next()) {//遍歷結(jié)果集rs.next()返回true表示有數(shù)據(jù) int id = rs.getInt("id"); // 獲取編號id String bookName = rs.getString("bookName"); // 獲取圖書名稱 bookName float price = rs.getFloat("price"); // 獲取圖書價格 price String author = rs.getString("author"); // 獲取圖書作者 author int bookTypeId = rs.getInt("bookTypeId"); // 獲取圖書類別id //---- 大數(shù)據(jù)字符集,一般存儲大文本等內(nèi)容 ---- //大數(shù)據(jù)類型,獲取圖書內(nèi)容, //這里演示mysql數(shù)據(jù)庫類型是longtext Clob c=rs.getClob("context"); //轉(zhuǎn)為字符串 String context=c.getSubString(1, (int)c.length()); //---大數(shù)據(jù)二進(jìn)制,一般存儲圖片,視頻,音頻等 ------- //創(chuàng)建一個輸出流, //將數(shù)據(jù)庫中的封面圖片保存到該路徑中c:/pic.jpg FileOutputStream out=new FileOutputStream(new File("c:/pic.jpg")); //大數(shù)據(jù)類型,獲取封面圖片, //這里演示mysql數(shù)據(jù)庫類型是longblod out.write(b.getBytes(1,(int)b.length())); Book book=new Book(id,bookName,price, author,bookTypeId);//封裝圖書對象 bookList.add(book);//將圖書對象保存集合中 } close(con,pstmt,rs);//關(guān)閉連接 return bookList; } /** * JDBC調(diào)用存儲過程演示 * 數(shù)據(jù)庫中有一個存儲過程為 pro_getBookNameById * 該存儲過程功能是通過編號(id)查詢圖書名稱(bookName) * 該存儲過程中有分別有一個輸入?yún)?shù) * 跟一個輸出參數(shù)(輸出參數(shù)名稱為 bN ) * @param id 圖書編號 * @return 返回圖書名稱 * @throws Exception */ private String getBookNameById(int id) throws Exception{ Connection con=getCon();// 獲取數(shù)據(jù)庫連接 //調(diào)用存儲過程語句,第一個為輸入?yún)?shù), //第二個是輸出參數(shù),輸出參數(shù)名稱是bN String sql="{CALL pro_getBookNameById(?,?)}"; //得到CallableStatement對象 CallableStatement cstmt=con.prepareCall(sql); cstmt.setInt(1, id);//設(shè)置第一個參數(shù)(即輸入?yún)?shù)) //設(shè)置返回類型(即輸出參數(shù)類型),指的是數(shù)據(jù)庫中的數(shù)據(jù)類型 cstmt.registerOutParameter(2, Types.VARCHAR); cstmt.execute();//執(zhí)行 //獲取返回值(即輸出參數(shù) bN ) String bookName=cstmt.getString("bN"); close(con,cstmt,null);//關(guān)閉連接 return bookName; } /** *元數(shù)據(jù)演示 */ public void demo () throws Exception { Connection con=getCon();//獲取數(shù)據(jù)庫鏈接對象 //---------- 元數(shù)據(jù) ---------------- //獲取DatabaseMetaData對象 DatabaseMetaData dmd=con.getMetaData(); //dmd.getDatabaseProductName()獲取數(shù)據(jù)庫名稱 System.out.println("數(shù)據(jù)庫名稱:"+dmd.getDatabaseProductName()); //getDriverMajorVersion()得到數(shù)據(jù)庫大版本號, //dmd.getDriverMinorVersion()得到數(shù)據(jù)庫小版本號 System.out.println("數(shù)據(jù)庫版本:"+dmd.getDriverMajorVersion()+"."+dmd.getDriverMinorVersion()); String sql="select * from t_book";//查詢語句 //得到PreparedStatement對象 PreparedStatement pstmt=con.prepareStatement(sql); // 獲取元數(shù)據(jù)列的總數(shù)(即有多少列(字段)) int num=rsmd.getColumnCount(); System.out.println("共有"+num+"列"); for(int i=1;i<=num;i++){ //rsmd.getColumnName(i)獲取第i列的列名稱(即字段名稱), //rsmd.getColumnTypeName(i)獲取第i列的數(shù)據(jù)類型 System.out.println(rsmd.getColumnName(i)+"," +rsmd.getColumnTypeName(i)); } } /** * *事務(wù)演示 **/ public static void main(String[] args) throws Exception { Connection con=null; Savepoint sp=null; try { con=getCon();//得到數(shù)據(jù)庫連接 //取消自動提交(將事務(wù)設(shè)置為手動提交) con.setAutoCommit(false); System.out.println("張三開始向李四轉(zhuǎn)賬!"); int account=500; outCount(con, "張三", account);//此處假設(shè)轉(zhuǎn)出賬操作 //演示:設(shè)置一個保存點(即數(shù)據(jù)庫的備份點) //sp=con.setSavepoint(); inCount(con, "李四", account);//此處假設(shè)轉(zhuǎn)入賬操作 System.out.println("轉(zhuǎn)賬成功!"); con.commit(); //提交事務(wù) } catch (Exception e) { con.rollback(); //回滾事務(wù) //con.rollback(sp);//回滾事務(wù)到sp保存點 e.printStackTrace();//打印錯誤信息 }finally{ con.close();//關(guān)閉連接 } } /** * 假設(shè)轉(zhuǎn)出操作 * @param con 數(shù)據(jù)庫連接 * @param accountName 賬戶 * @param account 金額 * @throws Exception */ private static void outCount(Connection con,String accountName,int account)throws Exception{ String sql="update t_account set accountBalance=accountBalance-account? where accountName=?";// PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setInt(1, account); pstmt.setString(2, accountName); pstmt.executeUpdate(); } /** * 假設(shè)轉(zhuǎn)入操作 * @param con 數(shù)據(jù)庫連接 * @param accountName 賬戶 * @param account 金額 * @throws Exception */ private static void inCount(Connection con,String accountName,int account)throws Exception{ String sql="update t_account set account=accountBalance+account? where accountName=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setInt(1, account); pstmt.setString(2, accountName); pstmt.executeUpdate(); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 延安市| 阳信县| 丰顺县| 贡觉县| 本溪市| 调兵山市| 武鸣县| 溧水县| 眉山市| 博白县| 墨竹工卡县| 加查县| 宁都县| 文登市| 池州市| 天等县| 远安县| 莲花县| 安远县| 尚义县| 永宁县| 琼海市| 丹凤县| 衡水市| 仁寿县| 阿克苏市| 株洲县| 兰坪| 喀喇沁旗| 南投市| 徐水县| 阳高县| 南安市| 旌德县| 时尚| 武功县| 舒兰市| 湛江市| 大荔县| 湟中县| 科技|