Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. try {
  2. String driver = "com.mysql.jdbc.Driver";
  3. String url = "jdbc:mysql://localhost:3306/project";
  4. Class.forName(driver);//load driver to memory
  5. Connection con = DriverManager.getConnection(url, "rootsql", "1234?");
  6.  
  7. if (con != null) {
  8.  
  9. String sqlString = "INSERT INTO table_jobs(name,content,sd,ed,status,creation_date) values('"+name+"','"+content+"','"+sd+"','"+ed+"','"+status+"','"+creation_date+"');";
  10. Statement stmt = con.createStatement();
  11. idreturned = stmt.executeUpdate(sqlString , Statement.RETURN_GENERATED_KEYS);
  12.  
  13. ResultSet rs = stmt.getGeneratedKeys();
  14. if (rs.next()){
  15. idreturned = rs.getInt(1);
  16. }
  17. rs.close();
  18.  
  19. // here i try to insert the job for each worker for my third table
  20.  
  21.  
  22. String[] ary = ary_workers.split("-");
  23. if (ary.length>0){
  24. StringBuilder sb = new StringBuilder(ary_workers);
  25. Statement stm2 ;
  26. String sqlString2 ;
  27.  
  28. for (int i = 0 ; i < ary.length ; i++ ){
  29. sqlString2 = "INSERT INTO table_ids(idtable_jobs,idtable_workers) values('"+idreturned+"','"+1+"');";
  30. stm2 = con.createStatement();
  31. stm2.execute(sqlString2);
  32. }
  33. }
  34.  
  35. con.close();
  36. return "genrated = "+ idreturned;
  37. }
  38. if (con == null) {
  39. return "no connection";
  40. }
  41. } catch (Exception e) {
  42. e.getLocalizedMessage();
  43. outst = e.toString();
  44. }
  45. return outst;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement