摘要:知識點總結(jié)連接步驟及知識點總結(jié)連接數(shù)據(jù)庫步驟依序關(guān)閉使用的對象連接操作加載對應(yīng)驅(qū)動建立連接連接對象內(nèi)部包含了對象,是一個遠程連接。比較耗時這是對象管理的一個要點真正開發(fā)中,為了提高效率,都會使用連接池來管理連接對象張柏芝女張三執(zhí)行結(jié)果
Java知識點總結(jié)(JDBC-連接步驟及CRUD)
@(Java知識點總結(jié))[Java, JDBC]
連接數(shù)據(jù)庫步驟依序關(guān)閉使用的對象連接:
ResultSet -> Statement -> Connection
CRUD操作import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StudentDao { private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123"; Connection conn = null; try { Class.forName(driver); //classLoader,加載對應(yīng)驅(qū)動 //建立連接(連接對象內(nèi)部包含了Socket對象,是一個遠程連接。比較耗時!這是Connection對象管理的一個要點!) //真正開發(fā)中,為了提高效率,都會使用連接池來管理連接對象! conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } private static int insert(User user) { Connection conn = getConn(); int i = 0; String sql = "insert into users (Name,Sex,Age) values(?,?,?)"; PreparedStatement pstmt = null; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, user.getName()); pstmt.setString(2, user.getSex()); pstmt.setInt(3, user.getAge()); i = pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return i; } private static int update(User user) { Connection conn = getConn(); int i = 0; String sql = "update users set Age="" + user.getAge() + "" where Name="" + user.getName() + """; PreparedStatement pstmt = null; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return i; } private static Integer getAll() { Connection conn = getConn(); String sql = "select * from users"; PreparedStatement pstmt =null; ResultSet rs = null; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("============================"); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + " "); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print(" "); } } System.out.println(""); } System.out.println("============================"); } catch (SQLException e) { e.printStackTrace(); }finally { try { rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; } private static int delete(String name) { Connection conn = getConn(); int i = 0; String sql = "delete from users where Name="" + name + """; PreparedStatement pstmt = null; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return i; } public static void main(String[] args) { getAll(); insert(new User("張柏芝", "女", 43)); getAll(); update(new User("張三", "", 38)); delete("Achilles"); getAll(); } }
public class User { private int id; private int age; private String name ; private String sex; public User( String name, String sex,int age) { this(); this.age = age; this.name = name; this.sex = sex; } public User() { } public int getAge() { return age; } public int getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } public void setAge(int age) { this.age = age; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } }
執(zhí)行結(jié)果:
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71427.html
摘要:前言由于寫的文章已經(jīng)是有點多了,為了自己和大家的檢索方便,于是我就做了這么一個博客導(dǎo)航。 前言 由于寫的文章已經(jīng)是有點多了,為了自己和大家的檢索方便,于是我就做了這么一個博客導(dǎo)航。 由于更新比較頻繁,因此隔一段時間才會更新目錄導(dǎo)航哦~想要獲取最新原創(chuàng)的技術(shù)文章歡迎關(guān)注我的公眾號:Java3y Java3y文章目錄導(dǎo)航 Java基礎(chǔ) 泛型就這么簡單 注解就這么簡單 Druid數(shù)據(jù)庫連接池...
摘要:使用需要使用作為事務(wù)管理器。兩個事務(wù)互不影響。這是默認的隔離級別,使用數(shù)據(jù)庫默認的事務(wù)隔離級別下邊的四個與的隔離級別相對應(yīng)這是事務(wù)最低的隔離級別,它充許另外一個事務(wù)可以看到這個事務(wù)未提交的數(shù)據(jù)。這種事務(wù)隔離級別可 Spring事務(wù)整理 工作了幾年了,今天抽時間整理一下spring的事務(wù),說起spring的事務(wù)是面試的時候面試官經(jīng)常提及的問題,接下來結(jié)合網(wǎng)上資料再總結(jié)下spring的事務(wù)...
摘要:忽略該字段的映射省略創(chuàng)建數(shù)據(jù)訪問層接口,需要繼承,第一個泛型參數(shù)是實體對象的名稱,第二個是主鍵類型。 SpringBoot 是為了簡化 Spring 應(yīng)用的創(chuàng)建、運行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個 WEB 工程 上一篇介紹了Spring JdbcTempl...
摘要:數(shù)據(jù)庫連接池什么是數(shù)據(jù)庫連接池簡單來說數(shù)據(jù)庫連接池就是提供連接的。。。 1.數(shù)據(jù)庫連接池 什么是數(shù)據(jù)庫連接池 簡單來說:數(shù)據(jù)庫連接池就是提供連接的。。。 為什么我們要使用數(shù)據(jù)庫連接池 數(shù)據(jù)庫的連接的建立和關(guān)閉是非常消耗資源的 頻繁地打開、關(guān)閉連接造成系統(tǒng)性能低下 編寫連接池 編寫連接池需實現(xiàn)java.sql.DataSource接口 創(chuàng)建批量的Connection用Linke...
閱讀 3279·2021-11-12 10:36
閱讀 1417·2019-08-30 15:56
閱讀 2527·2019-08-30 11:26
閱讀 600·2019-08-29 13:00
閱讀 3646·2019-08-28 18:08
閱讀 2788·2019-08-26 17:18
閱讀 1944·2019-08-26 13:26
閱讀 2471·2019-08-26 11:39