Eclipse中java向資料庫中添加資料

來源:互聯網
上載者:User

標籤:類型   eclipse   rman   password   lock   let   dex   編譯   注意事項   

前面詳細寫過如何串連資料庫的具體操作,下面介紹向資料庫中添加資料。

注意事項:如果參考下面代碼,需要

改包名,資料庫名,資料庫帳號,密碼,和資料表(資料表裡面的資訊)

 

 1 package com.ningmeng; 2  3 import java.sql.*; 4  5 /** 6  * 1:向資料庫中添加資料 7  * @author biexiansheng 8  * 9  */10 public class Test01 {11 12     public static void main(String[] args) {13         try {14             Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動15             System.out.println("載入資料庫驅動成功");16             String url="jdbc:mysql://localhost:3306/test";//聲明資料庫test的url17             String user="root";//資料庫的使用者名稱18             String password="123456";//資料庫的密碼19             //建立資料庫連接,獲得連線物件conn(拋出異常即可)20             Connection conn=DriverManager.getConnection(url, user, password);21             System.out.println("串連資料庫成功");22             //產生一條mysql語句23             String sql="insert into users(username,password,age,sex) values(‘小別‘,‘123456‘,22,0)";        24             Statement stmt=conn.createStatement();//建立一個Statement對象25             stmt.executeUpdate(sql);//執行sql語句26             System.out.println("插入到資料庫成功");27             conn.close();28             System.out.println("關閉資料庫成功");29         } catch (ClassNotFoundException e) {30             // TODO Auto-generated catch block31             e.printStackTrace();32         }//33         catch (SQLException e) {34             // TODO Auto-generated catch block35             e.printStackTrace();36         }37         38     }39     40 }

 

詳細運行結果

這樣就可以完美插入資料,增刪改查第一步完美解決。

簡單介紹一下所使用的知識點:

在java程式中一旦建立了資料庫的串連,就可以使用Connection介面的createStatement()方法來獲得statement對象

然後通過excuteUpdate()方法來執行sql語句,就可以向資料庫中添加資料了。

1:createStatement()方法是Connection介面的方法,用來建立Statement對象

2:Connection介面代表和特定的資料庫連接,要對資料庫中資料表的資料進行操作,首先要擷取資料庫連接。

3:Statement介面用於建立向資料庫中傳遞SQL語句的對象,該介面提供了一些方法可以實現對資料庫的常用操作。

4:Statement介面中的excuteUpdate()方法執行給定的SQL語句,該語句可以是INSERT,UPDATE,DELETE語句。

 

 

 

第二種方法

使用PreparedStatement介面向mysql資料庫中插入資料

 

 1 package com.ningmeng; 2  3 import java.sql.*; 4  5 /** 6  * 1:使用PreparedStatement介面來執行插入語句 7  *  8  * @author biexiansheng 9  *10  */11 public class Test02 {12 13     public static void main(String[] args) {14         // TODO Auto-generated method stub15         try {16             Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動17             System.out.println("載入資料庫驅動成功");18             String url="jdbc:mysql://localhost:3306/test";//聲明資料庫test的url19             String user="root";//資料庫使用者名稱20             String password="123456";//資料庫密碼21             //建立資料庫連接,獲得連線物件conn22             Connection conn=DriverManager.getConnection(url, user, password);23             System.out.println("串連資料庫驅動成功");24             //產生一條SQL語句25             String sql="insert into users(username,password,age,sex) values(?,?,?,?)";26             PreparedStatement ps=conn.prepareStatement(sql);//建立一個Statement對象27             ps.setNString(1,"lisi");//為sql語句中第一個問號賦值28             ps.setString(2,"123456");//為sql語句中第二個問號賦值29             ps.setInt(3,24);//為sql語句第三個問號賦值30             ps.setInt(4,2);//為sql語句的第四個問號賦值31             ps.executeUpdate();//執行sql語句32             conn.close();//關閉資料庫連接對象33             System.out.println("關閉資料庫連接對象");34         } catch (ClassNotFoundException e) {35             // TODO Auto-generated catch block36             e.printStackTrace();37         } catch (SQLException e) {38             // TODO Auto-generated catch block39             e.printStackTrace();40         }41         42         43     }44 45 }

 

 

 

由於剛才不小心多執行了一遍第一個程式,所以多了一行id==7的,特此注釋一下

1:PreparedStatement介面繼承Statement,用於執行動態SQL語句,通過PreparedStatement執行個體執行SQL語句,將被先行編譯並且儲存到PreparedStatement執行個體中,從而可以反覆的執行該SQL語句。

2:PreparementStatement介面中的方法,如executeUpdate在此PrepareStatement對象中執行sql語句,該sql語句必須是一個INSERT.UPDATE,DELETE語句,或者是沒有傳回值的DDL語句。

3:setString(int pIndex,String str)將參數pIndex位置上設定為給定的String類型的參數,俗語就是在第幾個位置寫上符合的資料類型

setInt(int pIndex,int x)

 

 

其他的都類似,不作多敘述

 

Eclipse中java向資料庫中添加資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.