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