Database connection to Java

Source: Internet
Author: User

Jdbc:java database connection, which is the Java connection.
Function: Completes the interaction between the database data and the memory data.

In order to block differences between different databases, an interface standard has been established between the memory and the various databases. Each vendor implements the interface class according to the standard of the interface.


JDBC is a standard set of Java connection databases. The standard defines a series of interfaces, which are provided by the database vendor in accordance with the characteristics of its own database, which is called by the developer. Developers can block differences between different database vendors based on the interface invocation method. In this way, whatever database is connected is a set of APIs.

JDBC Operation steps: Flow operation steps:
① load driver, establish connection 1, establish flow
② Execute SQL Statement 2, Operation Flow
③ close Connection 3, close

SQL injection: When the SQL statement is executed, because the value of the SQL statement is input by the user, it is received as a variable, if the SQL statement is executed in a concatenation string, if there are illegal characters or keywords in the data, it can result in a syntax error, or the execution result is incorrect, this is called SQL injection.


The difference between statement and PreparedStatement:

Statement is the parent interface of PreparedStatement. When executing SQL statements, you can only stitch the values in a concatenation string. will cause SQL injection. and low efficiency.

PreparedStatement, which is a precompiled SQL statement execution object that supports placeholder mode, does not cause SQL injection, regardless of the value of the data, when the string is processed. and high efficiency.

Connection syntax for Java and database:

1 @Override2      Public voidAdd (Manbean bean) {3         4         //Connection Object5Connection con=NULL;6         //SQL statement Execution Object7PreparedStatement ps=NULL;8         //Load Driver9         Try {TenClass.forName ("Org.gjt.mm.mysql.Driver"); One             //establish connection, localhost for host IP address (native), 3306 for MySQL port number, A             //TestDB is the library name of the database, and Characterencoding=utf-8 is the byte encoding set -             //root is the MySQL login name, 123456 is the MySQL login password -Con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/testdb?characterencoding=utf-8", the                     "Root","123456"); -System. out. println (con); -              -             //Execute SQL statement,? As placeholder +Ps=con.preparestatement ("INSERT INTO T_man (Manname,birthday,money) VALUES (?,?,?)"); -             //padding placeholders +Ps.setstring (1, Bean.getname ()); APs.setdate (2, Bean.getbirthday ()); atPs.setint (3, Bean.getmoney ()); -             //Update Database - ps.executeupdate (); -}Catch(Exception e) { - e.printstacktrace (); -         } in         finally{//Close Connection -             Try { to ps.close (); + con.close (); -}Catch(Exception e) { the                 //TODO auto-generated Catch block * e.printstacktrace (); $             }Panax Notoginseng         } -}

Database connection to Java

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.