jdbc-增刪改查

package com.neuedu.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.junit.Test;

public class Insert {

// 查詢 select select * from 表名 where 字段名=''

@Test

public void select() throws ClassNotFoundException, SQLException {

/*

* 創(chuàng)建jdbc應用步驟分7步 1)載入jdbc驅(qū)動(指定我要連接到哪種數(shù)據(jù)庫,連接不同數(shù)據(jù)庫用不同驅(qū)動)

*

* 2)定義連接url(連接準備 url,端口,用戶名,密碼) 3)建立連接 4)創(chuàng)建PreparedStatement(動態(tài))

* Statement(靜態(tài)的)(拼sql語句) 5)執(zhí)行數(shù)據(jù)庫命令(crud) 6)結(jié)果的處理 7)關閉連接

*/

// com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();

// 1)載入jdbc驅(qū)動(指定我要連接到哪種數(shù)據(jù)庫,連接不同數(shù)據(jù)庫用不同驅(qū)動)

Class.forName("com.mysql.jdbc.Driver");// (Drive:驅(qū)動)

// 2)定義連接url(連接準備 url,端口,用戶名,密碼)

String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";//數(shù)據(jù)庫地址

String user = "root";//數(shù)據(jù)庫用戶名

String password = "root";//數(shù)據(jù)庫密碼

// 3)建立連接

Connection conn = DriverManager.getConnection(url, user, password);

// 4)創(chuàng)建PreparedStatement(動態(tài)) Statement(靜態(tài)的)(拼sql語句)

Statement st = conn.createStatement();

// 5)執(zhí)行數(shù)據(jù)庫命令(crud)

// executeUpdate insert update delete

// executeQuery select 用于查詢

ResultSet rs = st.executeQuery("select * from emp where deptno=10");// 用ResultSet接,返回結(jié)果集

// 6)結(jié)果的處理

while (rs.next()) {// 不知道有幾條 用while循環(huán)

// 雙引號里是數(shù)據(jù)庫的字段名

int emp_no = rs.getInt("empno");

System.out.println(emp_no);

String e_name = rs.getString("ename");

System.out.println(e_name);

int de_ptno = rs.getInt("deptno");

System.out.println(de_ptno);

}

// 7)關閉連接

rs.close();

st.close();

conn.close();

}

// 發(fā)現(xiàn)異常不顯示,改造代碼

@Test

public void select1() {

Connection conn = null;

Statement st = null;

ResultSet rs = null;

try {

// 1)載入jdbc驅(qū)動(指定我要連接到哪種數(shù)據(jù)庫,連接不同數(shù)據(jù)庫用不同驅(qū)動)

Class.forName("com.mysql.jdbc.Driver");

// 2)定義連接url(連接準備 url,端口,用戶名,密碼)

String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

String user = "root";

String password = "root";

// 3)建立連接

conn = DriverManager.getConnection(url, user, password);

// 4)創(chuàng)建PreparedStatement(動態(tài)) Statement(靜態(tài)的)(拼sql語句)

st = conn.createStatement();

// 5)執(zhí)行數(shù)據(jù)庫命令(crud)

// executeUpdate insert update delete

// executeQuery select

// 查詢10號部門的員工的員工編號,員工姓名,部門編號

rs = st.executeQuery("select empno,ename,deptno from emp where deptno=10");

// 6)結(jié)果的處理

while (rs.next()) {

// 雙引號里是數(shù)據(jù)庫里的字段名,

String e_name = rs.getString("ename");

System.out.println(e_name);

int emp_no = rs.getInt("empno");

System.out.println(emp_no);

int deptno = rs.getInt("deptno");

System.out.println(deptno);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

// 7)關閉連接

try {

rs.close();

st.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

// 刪除 delete from 表名 where 字段名=''

@Test

public void del() {

Connection conn = null;

Statement st = null;

try {

// 1)載入jdbc驅(qū)動(指定我要連接到哪種數(shù)據(jù)庫,連接不同數(shù)據(jù)庫用不同驅(qū)動)

Class.forName("com.mysql.jdbc.Driver");

// 2)定義連接url(連接準備 url,端口,用戶名,密碼)

String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

String user = "root";

String password = "root";

// 3)建立連接

conn = DriverManager.getConnection(url, user, password);

// 4)創(chuàng)建PreparedStatement(動態(tài)) Statement(靜態(tài)的)(拼sql語句)

st = conn.createStatement();

// 5)執(zhí)行數(shù)據(jù)庫命令(crud)

// executeUpdate insert update delete

// executeQuery select

// 查詢10號部門的員工的員工編號,員工姓名,部門編號

int count = st.executeUpdate("delete from emp_copy where ename='SCOTT'");

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

} finally {

// 7)關閉連接

try {

st.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

// 修改 update 表名 set 字段名='' where 字段名=

@Test

public void update() throws ClassNotFoundException, SQLException {

Connection conn = null;

Statement st = null;

try {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url, user, password);

st = conn.createStatement();

int count = st.executeUpdate("update dept_copy set dname='java70' where dname='70'");

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

st.close();

conn.close();

} catch (SQLException e2) {

e2.printStackTrace();

}

}

}

// 添加 insert into 表名(,,) values(,,)

@Test

public void insert() throws ClassNotFoundException, SQLException {

Connection conn = null;

Statement st = null;

try {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url, user, password);

st = conn.createStatement();

int count = st.executeUpdate("insert into dept_copy(deptno,dname) values(50,'cx')");

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

st.close();

conn.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 本文內(nèi)容 1.什么是JDBC以及為什么要使用JDBC 2.JDBC核心API的講解 3.使用JDBC核心API進行...
    Vincilovfang閱讀 1,353評論 0 11
  • JDBC簡介 SUN公司為了簡化、統(tǒng)一對數(shù)據(jù)庫的操作,定義了一套Java操作數(shù)據(jù)庫的規(guī)范,稱之為JDBC。JDBC...
    奮斗的老王閱讀 1,638評論 0 51
  • 再見了王者榮耀,為了不再年輕的身體,為了需要陪伴的家人,為了正在被竊取的青春,為了拯救那日漸怠惰的靈魂…… ...
    左巴與禪閱讀 512評論 0 2
  • 林天拿著《天世》離開了一樓,到藏書閣外時他發(fā)現(xiàn)那老人依舊站在那但看自己的眼神有一點不同?!伴L老您貴姓”林天...
    弒魚閱讀 342評論 0 2
  • 很多時候我們對于生活或是工作或是感情總會有疑惑,我們感覺應該是對的,可是卻又總覺得差了一點點,卻很難當即找到真正的...
    今天安好閱讀 1,243評論 0 0

友情鏈接更多精彩內(nèi)容