Connection: prepareStatement(String sql) : Connection « java.sql « Java by API






Connection: prepareStatement(String sql)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {

  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:databaseName";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url,user, password);
      String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?";
      PreparedStatement updateLastName = connection.prepareStatement(changeLastName);

      updateLastName.setString(1, "Martin"); // Set lastname placeholder value
      updateLastName.setInt(2, 4); // Set author ID placeholder value

      int rowsUpdated = updateLastName.executeUpdate(); // execute the update
      System.out.println("Rows affected: " + rowsUpdated);
      connection.close();
    } catch (ClassNotFoundException cnfe) {
      System.err.println(cnfe);
    } catch (SQLException sqle) {
      System.err.println(sqle);
    }
  }
}

           
         
    
  








Related examples in the same category

1.Connection.TRANSACTION_NONE
2.Connection.TRANSACTION_READ_COMMITTED
3.Connection.TRANSACTION_READ_UNCOMMITTED
4.Connection.TRANSACTION_REPEATABLE_READ
5.Connection.TRANSACTION_SERIALIZABLE
6.Connection: close()
7.Connection: commit()
8.Connection: createStatement()
9.Connection: createStatement(int resultSetType, int resultSetConcurrency)
10.Connection: getAutoCommit()
11.Connection: getCatalog()
12.Connection: getMetaData()
13.Connection: getTransactionIsolation()
14.Connection: getWarnings()
15.Connection: nativeSQL(String sql)
16.Connection: prepareCall(String sql)
17.Connection: rollback()
18.Connection: rollback(Savepoint savepoint)
19.Connection: setAutoCommit(boolean autoCommit)
20.Connection: setSavepoint(String name)
21.Connection: setTransactionIsolation(int level)