Java之Mysql資料庫DML語句執行

來源:互聯網
上載者:User

標籤:java mysql dml

package py.db.com;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.PreparedStatement;public class DB_update {public static void main(String[] args) {//TestUpdate();TestUpdatePre();}private static void TestUpdatePre() {// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/test";String user = "root";String password = "uplooking";String sql1 = "update emp set sal = sal + ? where empno = 7900";String sql2 = "select ename from emp where hiredate<?";String sql3 = "update emp set sal = sal + 120 where empno = 7900";Connection conn = null;java.sql.PreparedStatement ps = null;try {//註冊並載入資料庫驅動Class.forName("com.mysql.jdbc.Driver");System.out.println("資料庫驅動載入成功...");//建立資料庫連接conn = DriverManager.getConnection(url, user, password);System.out.println("資料庫連接已建立...");//準備一個處理器用來封裝sql代碼ps = conn.prepareStatement(sql1);//設定參數ps.setInt(1, 250);//執行sql代碼int rs = ps.executeUpdate();if (rs == 1) {System.out.println("資料庫sql執行成功.");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("資料庫驅動載入失敗");e.printStackTrace();} catch (SQLException e) {System.out.println("資料庫通訊失敗");//e.printStackTrace();} finally {//關閉對象及串連try {ps.close();conn.close();} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("對象關閉失敗");//e.printStackTrace();}}}private static void TestUpdate() {// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/test";String user = "root";String password = "uplooking";String sql1 = "select ename,empno from emp where empno=? and sal>?";String sql2 = "select ename from emp where hiredate<?";String sql3 = "update emp set sal = sal + 120 where empno = 7900";Connection conn = null;Statement stmt = null;try {//註冊並載入資料庫驅動Class.forName("com.mysql.jdbc.Driver");System.out.println("資料庫驅動載入成功...");//建立資料庫連接conn = DriverManager.getConnection(url, user, password);System.out.println("資料庫連接已建立...");//準備一個處理器用來封裝sql代碼stmt = conn.createStatement();//執行sql代碼int rs = stmt.executeUpdate(sql3);if (rs == 1) {System.out.println("資料庫sql執行成功.");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("資料庫驅動載入失敗");e.printStackTrace();} catch (SQLException e) {System.out.println("資料庫通訊失敗");//e.printStackTrace();} finally {//關閉對象及串連try {stmt.close();conn.close();} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("對象關閉失敗");//e.printStackTrace();}}}}


本文出自 “影魔登場” 部落格,請務必保留此出處http://woodywoodpecker.blog.51cto.com/4820467/1649730

Java之Mysql資料庫DML語句執行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.