JDBC操作資料庫,比如修改電商資料庫中的分類的id,讓各商品隨機

來源:互聯網
上載者:User

標籤:null   imp   分類   操作   connect   java   cti   rgs   print   

 1 package CRM; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 10 11 public class JDBC {12     public static void main(String[] args) throws ClassNotFoundException, SQLException {13         //1、載入驅動14         Class.forName("com.mysql.jdbc.Driver");15         16         //2、建立串連17         String url01="jdbc:mysql://localhost:13306/ecshop?"18                 + "user=root&password=123456&allowMultiQuerises=true;";19          20         Connection con=null;21         try {22             con=DriverManager.getConnection(url01);23             System.out.println("建立成功");24         } catch (SQLException e) {25             System.out.println("建立失敗");26         }27         28         29         //3、操作控制代碼30         String sql="select goods_name from ecs_goods where goods_name like ‘測試%‘;",31                 sql1="select cat_id from ecs_category;";32                 //sql2="update ecs_goods set cat_id =${catid}  where goods_name";33         Statement stmt=null,stmt1=null,stmt2=null;34         try {35             stmt=con.createStatement();    36             stmt1=con.createStatement();37             stmt2=con.createStatement();38         } catch (SQLException e) {    39         }40         41         42         //進行資料庫查詢43         ResultSet rs=stmt.executeQuery(sql),rs1=stmt1.executeQuery(sql1);44         45         int n = rs1.getMetaData().getColumnCount(),n1 = rs.getMetaData().getColumnCount();46         //(1)擷取商品分類47         String s="";48         while (rs1.next()){49             String s1=rs1.getString(n);50             if (s==""){51                 s=s1;52             }else{53                 s=s+","+s1;54             }55         56         }57         System.out.println(s);58         String [] s1= s.split(","); 59         60         //(2)擷取需要修改的商品61         s="";62         63         while (rs.next()){64          String s2=rs.getString(n);65             if (s==""){66                 s=s2;67             }else{68                 s=s+","+s2;69             }70         71         }72         73         String [] s2= s.split(","); 74         System.out.println(s2[1]);75         76         //(3)隨機修改商品的分類77         for(int i = 0 ; i<s2.length;i++){78             79             stmt2.executeUpdate("update ecs_goods set cat_id =‘"+s1[(int) (Math.random()*s1.length)] 80                     +"‘where goods_name=‘"+s2[i]+"‘;");81             82         }                83         84         85         86     }87 }

 

JDBC操作資料庫,比如修改電商資料庫中的分類的id,讓各商品隨機

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.