日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

03-JDBC连接MySQL数据库【插入数据】

發(fā)布時間:2025/3/20 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 03-JDBC连接MySQL数据库【插入数据】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JDBC連接mysql–學(xué)習(xí)目錄:

地址:http://blog.csdn.net/baidu_37107022/article/details/72600018

1.實現(xiàn)插入步驟

前三個步驟:注冊、獲得連接,創(chuàng)建statement對象方法,見上一節(jié):

02-JDBC實戰(zhàn)–JDBC查詢數(shù)據(jù)庫MySQL–http://blog.csdn.net/baidu_37107022/article/details/72597975

2.使用jdbc向數(shù)據(jù)庫中插入數(shù)據(jù)

這里使用的是queryDemo數(shù)據(jù)庫,表格為demo1student,表中數(shù)據(jù)如下:

1)插入單個數(shù)據(jù)

SQL語法:insert into 表名 values (值1,值2,值3,…)

代碼演示

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties;import org.junit.Test;public class Test7 {//插入insert單個數(shù)據(jù)@Testpublic void insert1(){Connection connection=null;PreparedStatement ps=null;try {//1.registerClass.forName("com.mysql.jdbc.Driver");//2.getConnectionString url="jdbc:mysql://localhost:3306/queryDemo";Properties info=new Properties();info.put("user", "root");info.put("password", "123");connection=DriverManager.getConnection(url, info);//3.create StatementString sql="insert into demo1student (name,age,score) values(?,?,?)";ps=connection.prepareStatement(sql);ps.setString(1, "Mary");ps.setInt(2, 22);ps.setInt(3, 99);//4.excuteUpdateint resultSet=ps.executeUpdate();if(resultSet>0){//如果插入成功,則打印successSystem.out.println("Sucess");}else{//如果插入失敗,則打印FailureSystem.out.println("Failure");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5.關(guān)閉資源if(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }

表格數(shù)據(jù)變化:
1.運行前:

2.運行后:
特別說明:id是主鍵,并且設(shè)置了自動增長。因為之前刪除了id=9的數(shù)據(jù),所以這里從10開始,自動增長的規(guī)則見:MySQL【連接地址】


2)使用addbatch()–插入多個數(shù)據(jù)

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;import org.junit.Test;public class Test8 {@Testpublic void insert5(){Connection connection=null;PreparedStatement ps=null;try {Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/queryDemo";String user="root";String password="123";connection=DriverManager.getConnection(url, user, password);String sql="insert into demo1student (name,age,score) values(?,?,?),(?,?,?)";ps=connection.prepareStatement(sql);ps.setString(1, "Jane");ps.setInt(2, 25);ps.setInt(3, 100);ps.setString(4, "李磊");ps.setInt(5, 28);ps.setInt(6, 99);//4.excuteUpdateps.addBatch();int[] resultSet=ps.executeBatch();if(resultSet.length >0){//如果插入成功,則打印successSystem.out.println("Sucess");}else{//如果插入失敗,則打印FailureSystem.out.println("Failure");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5.關(guān)閉資源if(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }

表格數(shù)據(jù)變化:

1.運行前

2.運行后:


3)使用for循環(huán)–插入多個數(shù)據(jù)

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties;import org.junit.Test;public class Test9 {//使用for循環(huán)--插入insert多個數(shù)據(jù)@Testpublic void insert2(){Connection connection=null;PreparedStatement ps=null;try {Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/queryDemo";Properties info=new Properties();info.put("user", "root");info.put("password", "123");connection=DriverManager.getConnection(url, info);String sql="insert into demo1student (name,age,score) values(?,?,?)";ps=connection.prepareStatement(sql);int num=0;for(int i=0;i<5;i++){//這里我只是隨機賦值,可以將你想要添加的數(shù)據(jù)放在集合中,使用for插入數(shù)據(jù)//name賦值ps.setString(1, "汪洋"+i);//age賦值ps.setInt(2, 20+(int)Math.random()*5);//score賦值ps.setInt(3, 77+(int)Math.random()*20);num=ps.executeUpdate();}if(num>0){//如果插入成功,則打印successSystem.out.println("Sucess");}else{//如果插入失敗,則打印FailureSystem.out.println("Failure");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5.關(guān)閉資源if(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }

運行結(jié)果:

1.運行前

2.運行后

總結(jié)

以上是生活随笔為你收集整理的03-JDBC连接MySQL数据库【插入数据】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。