A summary of JDBC knowledge of programmers dealing with databases

Source: Internet
Author: User
Tags sql injection sql injection attack stmt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.