Wu Yuxiong - natural born JAVA Database Programming: Transaction Processing

DROP TABLE user ;    -- 删除表
CREATE TABLE user(
    id        INT        AUTO_INCREMENT PRIMARY KEY ,
    name        VARCHAR(30)    NOT NULL ,
    password     VARCHAR(32)    NOT NULL ,
    age        INT        NOT NULL ,
    sex        VARCHAR(2)    DEFAULT '男' ,
    birthday    DATE 
) ;

INSERT INTO user(name,password,age,sex,birthday) VALUES ('李康','www.mldnjava.cn',23,'女','2003-08-27') ;
Import the java.sql.Connection;
 Import the java.sql.DriverManager;
 Import java.sql.SQLException;
 Import the java.sql.Statement;
 public  class TranDemo01 {
     // database driver defines the MySQL 
    public  static  Final String dbDriver = "org.gjt .mm.mysql.Driver " ;
     // connection address defines the MySQL database 
    public  static  Final String dburl =" jdbc: MySQL: // localhost: 3306 / MLDN " ;
     // connect MySQL database user name 
    public  static  Final String = DBUSER "root" ;
     // MySQL database connection password
    public  static  Final String = DBPASS in "mysqladmin" ;
     public  static  void main (String args []) throws Exception {     // all exceptions thrown 
        Connection Conn = null ;         // database connection 
        the Statement stmt = null ;         // defined database operation 
        Class. the forName (dbDriver);     // load drivers 
        Conn = the DriverManager.getConnection (dburl, with DBUSER, DBPASS in); 
        stmt = conn.createStatement (); 
        stmt.addBatch ("The INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( '. 1-LXH', '. 1-Hello',. 11, 'M', '1975-03-05')" ); 
        stmt .addBatch ( "the INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( 'LXH-2', 'Hello-2', 12 is, 'F', '1976-03-05')" );
         // after " '" this SQL syntax error appeared, so, time will certainly execute this statement appears coding errors 
        stmt.addBatch ( "INSERT INTO user (name , password, age, sex, birthday)" + 
            "the VALUES ( 'LXH-'3', '. 3-Hello', 13 is, 'M', '1977-06-01')" ); 
        stmt.addBatch ( "the INSERT the INTO User (name, password, Age, Sex , birthday) "+
            " VALUES ('LXH-4','hello-4',14,'女','1965-03-05') ") ;
        stmt.addBatch("The INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( '. 5-LXH', '. 5-Hello', 15, 'M', '1965-08-05')" );
         int TEMP [] = stmt.executeBatch (); 
        System.out.println ( "updated:" + temp.length + ". pieces of data" ); 
        stmt.close (); 
        conn.Close ();             // close the database 
    } 
} ;
Import the java.sql.Connection;
 Import the java.sql.DriverManager;
 Import java.sql.SQLException;
 Import the java.sql.Statement;
 public  class TranDemo02 {
     // database driver defines the MySQL 
    public  static  Final String dbDriver = "org.gjt .mm.mysql.Driver " ;
     // connection address defines the MySQL database 
    public  static  Final String dburl =" jdbc: MySQL: // localhost: 3306 / MLDN " ;
     // connect MySQL database user name 
    public  static  Final String = DBUSER "root" ;
     // MySQL database connection password
    public  static  Final String = DBPASS in "mysqladmin" ;
     public  static  void main (String args []) throws Exception {     // all exceptions thrown 
        Connection Conn = null ;         // database connection 
        the Statement stmt = null ;         // defined database operation 
        Class. the forName (dbDriver);     // load drivers 
        Conn = the DriverManager.getConnection (dburl, with DBUSER, DBPASS in); 

        conn.setAutoCommit ( to false );     // cancel automatic submission 

        stmt =conn.createStatement (); 
        stmt.addBatch ( "the INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( '. 1-LXH' '. 1-Hello',. 11, 'M', '1975 -03-05 ') " ); 
        stmt.addBatch ( " the INSERT the INTO User (name, password, Age, Sex, Birthday) "+ 
            " the VALUES (' LXH-2 ',' Hello-2 ', 12 is,' F ' , '1976-03-05') " );
         // adding" ' "after that, this SQL syntax error appeared, therefore, this statement certainly appears when the execution of the code error 
        stmt.addBatch (" INSERT INTO user (name , password, Age, Sex, Birthday) "+ 
            " the VALUES ( 'LXH-'3', '. 3-Hello', 13 is, 'M', '1977-06-01') " ); 
        stmt.addBatch("INSERT INTO user(name,password,age,sex,birthday)" +) ;
        stmt.addBatch(
            "VALUES ( 'LXH-4' , 'hello-4', 14, ' M', '1965-03-05')" "the INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( 'LXH-5', 'hello -5', 15, ' M', '1965-08-05') " );
         the try {
             int TEMP [] = stmt.executeBatch (); 
            System.out.println ( " update a:. "+ temp.length +" pieces of data " ); 
            conn.commit ();     // successfully operated all 
        } the catch (Exception E) {
             the try { 
                conn.rollback ();
            }catch(Exception e1){
                
            }
        }
        stmt.close() ;
        conn.Close ();             // close the database 
     }
};
Import the java.sql.Connection;
 Import the java.sql.DriverManager;
 Import java.sql.SQLException;
 Import the java.sql.Statement;
 Import java.sql.Savepoint;
 public  class TranDemo03 {
     // database driver defines the MySQL 
    public  static  Final dbDriver = String "the org.gjt.mm.mysql.Driver" ;
     // connect MySQL database address defines 
    public  static  Final String dburl = "JDBC: MySQL: // localhost: 3306 / MLDN" ;
     // connecting user MySQL database name 
    public  static  Final String DBUSER = "root" ;
    // connection password MySQL database 
    public  static  Final String = DBPASS in "mysqladmin" ;
     public  static  void main (String args []) throws Exception {     // all exceptions thrown 
        Connection Conn = null ;         // database connection 
        the Statement stmt = null ;         // definition of database operations 
        the Class.forName (dbDriver);     // load drivers 
        Conn = the DriverManager.getConnection (dburl, with DBUSER, DBPASS in); 

        conn.setAutoCommit ( to false );     // cancel automatic submission

        stmt = conn.createStatement() ;
        stmt.executeUpdate("INSERT INTO user(name,password,age,sex,birthday)" +
            " VALUES ('LXH-1','hello-1',11,'男','1975-03-05') ") ;
        stmt.executeUpdate("INSERT INTO user(name,password,age,sex,birthday)" +
            " VALUES ('LXH-2','hello-2',12,'女','1976-03-05') ") ;
        Savepoint sp = conn.setSavepoint() ;        // 设置保存点
        
        stmt.executeUpdate("INSERT INTO user(name,password,age,sex,birthday)" +
            " VALUES ('LXH-4','hello-4',14,'女','1965-03-05') ") ;
        stmt.executeUpdate("The INSERT the INTO User (name, password, Age, Sex, Birthday)" + 
            "the VALUES ( '. 5-LXH', '. 5-Hello', 15, 'M', '1965-08-05')" );
         the try { 
            conn.rollback (SP);     // rollback to the savepoint 
            conn.commit ();     // All the operation was successful 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        stmt.close (); 
        Conn. Close ();             // close the database 
    } 
};

 

Guess you like

Origin www.cnblogs.com/tszr/p/12158912.html