JDBC brief summary

Several common database JDBC URL

  • For Oracle database connection takes the form:
    JDBC: Oracle: Thin: @localhost: 1521: SID
  • For SQLServer database connection takes the form:
    JDBC: Microsoft: SQLServer // localhost: 1433; SID = DatabaseName
  • For MYSQL database connection takes the form:
    JDBC: MySQL: // localhost: 3306 / SID

JDBC API

  • java.sql.DriverManager used to load the driver, get a database connection.
  • java.sql.Connection complete the connection to a specified database
  • java.sql.Statement in a given connection in a container declared to perform SQL, it contains two important sub-type.
  • Java.sql.PreparedSatement for precompiled sql statement execution
  • Java.sql.CallableStatement calling for the implementation of the database stored procedure
  • Java.sql.ResultSet way to get results for a given statement

PreparedStatement

  • The method can obtain PreparedStatement object by calling the Connection object preparedStatement ()
  • PreparedStatement interface is a sub-interface Statement, which represents a pre-compiled SQL statement
  • SQL statements PreparedStatement objects represent the parameters with a question mark (?) To represent, call the PreparedStatement object setXXX () methods to set these parameters. SetXXX () method takes two parameters, the first parameter is to be set SQL statement parameters index (starting from 1), a SQL statement is the second set value of the parameter in

PreparedStatement vs Statement

  • Readability and maintainability of the code.
  • PreparedStatement can increase the maximum possible performance:
    • DBServer precompiled statement will provide performance optimizations. Because pre-compiled statements are likely to be called repeatedly, so the statement is cached in the code execution after the compiler is DBServer, then the next is the same as long as you do not need to compile a prepared statement when you call, as long as the parameters directly into compiled code execution statement will be implemented.
    • In the statement the statement, even if the operation is the same content, but because the data is not the same, so the entire statement itself can not match, meaning no cache statement. The fact is that no database would execute code cache after the general statement compilation so every time it performs incoming statement compilation time.
    • (Syntax check, semantic checks, translated into binary commands, cache)
  • PreparedStatement can prevent SQL injection

Guess you like

Origin www.cnblogs.com/fly-book/p/11531619.html