03-JDBC连接MySQL数据库【插入数据】
生活随笔
收集整理的這篇文章主要介紹了
03-JDBC连接MySQL数据库【插入数据】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC連接mysql–學習目錄:
地址:http://blog.csdn.net/baidu_37107022/article/details/72600018
1.實現插入步驟
前三個步驟:注冊、獲得連接,創建statement對象方法,見上一節:
02-JDBC實戰–JDBC查詢數據庫MySQL–http://blog.csdn.net/baidu_37107022/article/details/72597975
2.使用jdbc向數據庫中插入數據
這里使用的是queryDemo數據庫,表格為demo1student,表中數據如下:
1)插入單個數據
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單個數據@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.關閉資源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();}}}} }表格數據變化:
1.運行前:
2.運行后:
特別說明:id是主鍵,并且設置了自動增長。因為之前刪除了id=9的數據,所以這里從10開始,自動增長的規則見:MySQL【連接地址】
2)使用addbatch()–插入多個數據
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.關閉資源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();}}}} }表格數據變化:
1.運行前
2.運行后:
3)使用for循環–插入多個數據
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循環--插入insert多個數據@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++){//這里我只是隨機賦值,可以將你想要添加的數據放在集合中,使用for插入數據//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.關閉資源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();}}}} }運行結果:
1.運行前
2.運行后
總結
以上是生活随笔為你收集整理的03-JDBC连接MySQL数据库【插入数据】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 02-JDBC连接MySQL数据库【查询
- 下一篇: 04-JDBC连接MySQL数据库【修改