Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- METODA:
- // a java preparedstatement example
- public static void updateContactTitleForCity (
- Connection conn,
- String contactTitle,
- String city
- )
- throws SQLException
- {
- try
- {
- // create our java preparedstatement using a sql update query
- PreparedStatement ps = conn.prepareStatement(
- "UPDATE Customers SET ContactTitle = ? WHERE City = ?");
- // set the preparedstatement parameters
- ps.setString(1, contactTitle);
- ps.setString(2, city);
- // call executeUpdate to execute our sql update statement
- ps.executeUpdate();
- ps.close();
- }
- catch (SQLException se)
- {
- // log the exception
- throw se;
- }
- }
- MAIN:
- String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
- "databaseName=NORTHWND;user=piotr;password=student";
- try {
- // Establish the connection.
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection(connectionUrl);
- stmt = con.createStatement();
- updateContactTitleForCity(con, "AAAA", "Berlin");
- }
- // Handle any errors that may have occurred.
- catch (Exception e) {
- e.printStackTrace();
- }
- finally {
- if (rs != null) try { rs.close(); } catch(Exception e) {}
- if (stmt != null) try { stmt.close(); } catch(Exception e) {}
- if (con != null) try { con.close(); } catch(Exception e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement