Java MySQL database DML statement execution

Source: Internet
Author: User

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 {//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 ENAME FROM EMP WHERE 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.