JDBC Operations database, such as modifying the ID of the classification in the e-commerce database, so that each product randomly

Source: Internet
Author: User
Tags stmt

1  PackageCRM;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 9 Ten  One  Public classJDBC { A      Public Static voidMain (string[] args)throwsClassNotFoundException, SQLException { -         //1. Load Driver -Class.forName ("Com.mysql.jdbc.Driver"); the          -         //2. Establish the connection -String url01= "Jdbc:mysql://localhost:13306/ecshop?" -+ "user=root&password=123456&allowmultiquerises=true;"; +           -Connection con=NULL; +         Try { Acon=drivermanager.getconnection (URL01); atSYSTEM.OUT.PRINTLN ("Build Success"); -}Catch(SQLException e) { -System.out.println ("Build Failed"); -         } -          -          in         //3. Operation Handle -String sql= "Select Goods_name from Ecs_goods where goods_name like ' Test% ';", tosql1= "select cat_id from Ecs_category;"; +                 //sql2= "Update ecs_goods set cat_id =${catid} where goods_name"; -Statement stmt=NULL, stmt1=NULL, stmt2=NULL; the         Try { *stmt=con.createstatement ();  $stmt1=con.createstatement ();Panax NotoginsengStmt2=con.createstatement (); -}Catch(SQLException e) { the         } +          A          the         //make a database query +ResultSet rs=stmt.executequery (SQL), rs1=stmt1.executequery (SQL1); -          $         intn = Rs1.getmetadata (). getColumnCount (), N1 =rs.getmetadata (). getColumnCount (); $         //(1) Get the product category -String s= ""; -          while(Rs1.next ()) { theString s1=rs1.getstring (n); -             if(s== ""){Wuyis=S1; the}Else{ -S=s+ "," +S1; Wu             } -          About         } $ System.out.println (s); -String [] s1= s.split (",");  -          -         //(2) Get the product you need to modify AS= ""; +          the          while(Rs.next ()) { -String s2=rs.getstring (n); $             if(s== ""){ thes=S2; the}Else{ theS=s+ "," +S2; the             } -          in         } the          theString [] s2= s.split (",");  AboutSystem.out.println (s2[1]); the          the         //(3) Classification of randomly modified products the          for(inti = 0; i<s2.length;i++){ +              -Stmt2.executeupdate ("update ecs_goods set cat_id = '" +s1[(int) (Math.random () *s1.length)] the+ "' Where Goods_name= '" +s2[i]+ "';");Bayi              the         }                 the          -          -          the     } the}

JDBC Operations database, such as modifying the ID of the classification in the e-commerce database, so that each product randomly

Related Article

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.