Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. class AddStudent
  2. {
  3.  
  4. public static void main (String[] args)
  5. {
  6. try
  7. {
  8.  
  9. Connection conn = DriverManager.getConnection(url,user,pass);
  10. Statement st = conn.createStatement();
  11. Scanner first = new Scanner(System.in);
  12. System.out.println(first.nextLine());
  13.  
  14.  
  15. String SQL = "INSERT INTO test VALUES ('Cathy', 'Jones')";
  16. st.executeUpdate(SQL);
  17.  
  18.  
  19.  
  20. conn.close();
  21.  
  22. } catch (Exception e) {
  23. System.err.println("Got an exception! ");
  24. System.err.println(e.getMessage());
  25. }
  26.  
  27. }
  28.  
  29. private static String url = "jdbc:mysql://localhost:3306/registrar";
  30.  
  31. Scanner first = new Scanner(System.in);
  32. String f = first.nextLine();
  33.  
  34. Scanner last = new Scanner(System.in);
  35. String l = last.nextLine();
  36.  
  37. String SQL = "INSERT INTO test VALUES ('" + f + "','" + l + "')";
  38. st.executeUpdate(SQL);
  39.  
  40. PreparedStatement ps = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
  41. ps.setString(1, f);
  42. ps.setString(2, l);
  43. ps.executeUpdate();
  44.  
  45. String firstName = "Cathy";
  46. String lastName = "Jones";
  47. String query= "INSERT INTO test VALUES (?,?)";
  48. PreparedStatement stmt = mysqlConnection.prepareStatement(query);
  49. stmt.setString(1,firstName);
  50. stmt.setString(2,lastName);
  51. stmt.executeUpdate(SQL);
  52.  
  53. Scanner scanner = new Scanner(System.in);
  54. String first = scanner.nextLine();
  55. String last = scanner.nextLine();
  56.  
  57. String SQL = "INSERT INTO test VALUES ('Cathy', 'Jones')";
  58. st.executeUpdate(SQL);
  59.  
  60. String SQL = String.format("INSERT INTO test VALUES ('%s', '%s')", first, last);
  61. st.executeUpdate(SQL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement