如何利用java对mysql数据库进行增删改查
生活随笔
收集整理的這篇文章主要介紹了
如何利用java对mysql数据库进行增删改查
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
java-mysql
- 前提:
- 增:
- 刪:
- 改:
- 查:
前提:
首先要對java與數(shù)據(jù)庫進(jìn)行連接哦!
連接步驟
https://blog.csdn.net/hanhanwanghaha/article/details/105716885
代碼如下:
增:
@Test//數(shù)據(jù)插入public void demo1() {Connection conn=null;Statement stmt=null;try {//注冊驅(qū)動Class.forName("com.mysql.jdbc.Driver");//創(chuàng)建連接conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");//執(zhí)行sql對象stmt=conn.createStatement();String sql="INSERT INTO person VALUES(NULL,'eee','741','小白')";//返回1個整型int i=stmt.executeUpdate(sql);if(i>0) {System.out.println("插入成功!");}}catch(Exception e){e.printStackTrace();}finally {//釋放資源if(conn!=null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}conn=null;}if(stmt!=null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}stmt=null;}}}刪:
@Test//數(shù)據(jù)刪除public void demo3() {Connection conn=null;Statement stmt=null;try {//注冊驅(qū)動Class.forName("com.mysql.jdbc.Driver");//建立連接conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");//創(chuàng)建sql語句String sql="delete from person where id=5";stmt=conn.createStatement();int i=stmt.executeUpdate(sql);if(i>0) {System.out.println("刪除成功!");}}catch(Exception e) {e.printStackTrace();}finally {//釋放資源if(conn!=null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}conn=null;}if(stmt!=null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}stmt=null;}}}改:
@Test//數(shù)據(jù)更新public void demo2() {Connection conn=null;Statement stmt=null;try {//注冊驅(qū)動Class.forName("com.mysql.jdbc.Driver");//建立連接conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");//創(chuàng)建sql語句String sql="update person set username='qqq',password='852',address='小陳' where id=3";//創(chuàng)建sql執(zhí)行對象stmt=conn.createStatement();int i=stmt.executeUpdate(sql);if(i>0) {System.out.println("修改成功");}else {System.out.println("修改失敗");}}catch(Exception e) {e.printStackTrace();}finally {//釋放資源if(conn!=null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}conn=null;}if(stmt!=null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}stmt=null;}}}查:
@Testpublic void demo2() {try {//1.加載驅(qū)動DriverManager.registerDriver(new Driver());//2.創(chuàng)建連接Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name", "root", "123456");//是不是自己的賬戶和密碼//3.1創(chuàng)建sql語句對象String sql="select * from person";Statement stmt=conn.createStatement();//3.2執(zhí)行sql語句并進(jìn)行遍歷ResultSet resultSet=stmt.executeQuery(sql);while(resultSet.next()) {int uid=resultSet.getInt("id");String username=resultSet.getString("username");String password=resultSet.getString("password");String address=resultSet.getString("address");System.out.println(uid+" "+username+" "+password+" "+address+" ");}//4.釋放相關(guān)資源resultSet.close();stmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}注意:我的數(shù)據(jù)庫為name,表名為person,用戶名為root,密碼為123456,你需要根據(jù)自己的表進(jìn)行改正。
希望對你有用!
https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關(guān)注!
歡迎關(guān)注微信公眾號:寶藏女孩的成長日記
如有轉(zhuǎn)載,請注明出處(如不注明,盜者必究)
總結(jié)
以上是生活随笔為你收集整理的如何利用java对mysql数据库进行增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj 2406 还是KMP的简单应用
- 下一篇: 字典超详细--python