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&NBSP;ENAME&NBSP;FROM&NBSP;EMP&NBSP;WHERE&NBSP;HIREDATE<?"; string sql3 = "Update emp set sal = sal + 120 where empno = 7900 "; Connection conn = null;java.sql.preparedstatement ps = null;try {//register and load the database driver Class.forName ("Com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Database driver loaded successfully ...");//Establish Database connection Conn = drivermanager.getconnection (url, user, password); SYSTEM.OUT.PRINTLN ("Database connection established ...");//Prepare a processor to wrap the SQL code ps = conn.preparestatement (SQL1);//Set Parameters Ps.setint (1, 250);//Execute SQL Code int rs = ps.executeupdate ();if (rs == 1) { SYSTEM.OUT.PRINTLN ("Database SQL execution succeeded.");}} catch (classnotfoundexception e) {// TODO Auto-generated catch BLOCKSYSTEM.OUT.PRINTLN ("Database driver loading failed"); E.printstacktrace ();} catch (sqlexception e) {system.out.println ("Database communication Failed");//e.printstacktrace ();} finally {//close objects and connect try {ps.close (); Conn.close ();} catch (exception e) {// TODO Auto-generated catch BlockSystem.out.println ("Object shutdown failed");//e.printstacktrace ();}}} Private static void testupdate () &NBSp {// 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&NBSP;ENAME&NBSP;FROM&NBSP;EMP&NBSP;WHERE&NBSP;HIREDATE<?"; string sql3 = "Update emp set sal = sal + 120 where empno = 7900 "; connection conn = null; statement stmt = null;try {//register and load the database driver Class.forName ("Com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Database driver loaded successfully ...");//Establish Database connection Conn = drivermanager.getconnection (url, user, password); SYSTEM.OUT.PRINTLN ("Database connection established ...");//Prepare a processor to wrap SQL code stmt = conn.createstatement ();//Execute SQL code int Rs = stmt.executeupdate (SQL3);if (rs == 1) {sysTEM.OUT.PRINTLN ("Database SQL execution succeeded.");}} catch (classnotfoundexception e) {// TODO Auto-generated catch BLOCKSYSTEM.OUT.PRINTLN ("Database driver loading failed"); E.printstacktrace ();} catch (sqlexception e) {system.out.println ("Database communication Failed");//e.printstacktrace ();} finally {//close objects and connect try {stmt.close (); Conn.close ();} catch (exception e) {// TODO Auto-generated catch BlockSystem.out.println ("Object shutdown failed");//e.printstacktrace ();}}}
This article is from the "shadow debut" blog, please be sure to keep this source http://woodywoodpecker.blog.51cto.com/4820467/1649730
Java MySQL database DML statement execution