Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. METODA:
  2. // a java preparedstatement example
  3. public static void updateContactTitleForCity (
  4. Connection conn,
  5. String contactTitle,
  6. String city
  7. )
  8. throws SQLException
  9. {
  10. try
  11. {
  12. // create our java preparedstatement using a sql update query
  13. PreparedStatement ps = conn.prepareStatement(
  14. "UPDATE Customers SET ContactTitle = ? WHERE City = ?");
  15.  
  16. // set the preparedstatement parameters
  17. ps.setString(1, contactTitle);
  18. ps.setString(2, city);
  19. // call executeUpdate to execute our sql update statement
  20. ps.executeUpdate();
  21. ps.close();
  22. }
  23. catch (SQLException se)
  24. {
  25. // log the exception
  26. throw se;
  27. }
  28. }
  29.  
  30.  
  31.  
  32. MAIN:
  33.  
  34. String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
  35. "databaseName=NORTHWND;user=piotr;password=student";
  36.  
  37. try {
  38. // Establish the connection.
  39. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  40. con = DriverManager.getConnection(connectionUrl);
  41.  
  42. stmt = con.createStatement();
  43. updateContactTitleForCity(con, "AAAA", "Berlin");
  44. }
  45.  
  46. // Handle any errors that may have occurred.
  47. catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. finally {
  51. if (rs != null) try { rs.close(); } catch(Exception e) {}
  52. if (stmt != null) try { stmt.close(); } catch(Exception e) {}
  53. if (con != null) try { con.close(); } catch(Exception e) {}
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement