1.JDBC Full Name: Java database Connectivity,java connection.
(1) JDBC is a Java API for executing SQL statements that provides multiple, unified access to a variety of relational databases, consisting of classes and interfaces written in a set of Java languages.
(2) The Programmer JDBC API program can access all databases.
(3) With the Java language and JDBC, programmers do not have to write different applications for different platforms, just write the program once to make it run on any platform.
(4) JDBC Simple to do three things: establish a connection with the database, send, process the database statements and process the results.
3. Load the Oracle JDBC driver
Class.forName ("Oracle.jdbc.driver.OracleDriver")
4.execute, ExecuteQuery, executeupdate
ExecuteQuery: Returns the result set (ResultSet), typically used for SELECT statements.
Executeupdate: Returns the number of rows (int) Affected by this operation, typically used for insert,update,delete statements.
Execute returns a Boolean value, typically used for the Insert,update,delete statement.
Typical code for 5.RESULTSET processing:
while (Rs.next ()) {
String ename = rs.getstring (1);
int empno = Rs.getint ("Empno");
}
6.JDBC Chronicles 4 Big steps
(1) loading a driver drive;
(2) Creating a database connection (Connection)
(3) Create SQL command Publisher statement, send commands via statement and get results.
(4) Processing results (SELECT statement and ResultSet), closing the database resources after processing is complete.
7.SQL Injection Attack
For example, JDBC completes the user's login function
(1) SQL statement using string concatenation technique String sql = "SELECT * from T_user where userno= '" +userno+ "' and password =+upwd+" ";
(2) then use the SQL command Publisher to send the SQL command and get the result:
Stmt.executequery (SQL);
(3) If input: Userno = "Abc:,password =" abc ' or ' 1 ' = 1 ";
The above SQL string becomes sql = "SELECT * from t_user where Userno = ' abc ' and password = ' abc ' or ' 1 ' = ' 1";
The user name and password for this statement are not correct, but still have access to the data table, so there is a risk that this is called a SQL injection attack.
(4) Solution: Use statement sub-interface PreparedStatement to achieve.
* * Readability is not cumbersome
safe.
* More than 3 execution of the same SQL statement, high efficiency
Cases:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", "Scott", "Tiger");
stmt = Conn.createstatement ();
String sql = "SELECT * from EMP";
rs = stmt.executequery (SQL);
A summary of JDBC knowledge of programmers dealing with databases