JDBC Database connection

Source: Internet
Author: User

/******************** test using temporary data source code ***************/string URL = "Jdbc:mysql://localhost:3306/test";   String username = "root"; String password = "root"; Connection con = drivermanager.getconnection (URL, username, password); /********************JDBC Connection Database Operation ********************************/<span style= "White-space:pre" ></span >//(1) First load the driver of the database you want to connect to the JVM (Java Virtual machine), <span style= "White-space:pre" ></span>// This is achieved Class.forName ("Com.mysql.jdbc.Driver") through the static method forname (String className) of the Java.lang.Class class, and/or (2) obtaining a database connection/*** for example: (   MySQL connection URL) localhost:3306/test?useunicode=true&characterencoding=gbk; Useunicode=true: Indicates the use of the Unicode character set. If Characterencoding is set to gb2312 or GBK, this parameter must be set to true. CHARACTERENCODING=GBK: The character encoding method.    /string url = "Jdbc:mysql://localhost:3306/test";   String username = "root"; String password = "root"; Connection con = drivermanager.getconnection (URL, username, password);  (3) Create Statement object Statement stmt = Con.createstatement (); Normal state.The ment object is not recommended for use.   PreparedStatement pstmt = con.preparestatement (sql);   can prevent SQL injection callablestatement cstmt = Con.preparecall ("{Call Demosp (?,?)}"); Call the stored procedure//(4) Execute SQL statement String sqlstring= "SQL"; ResultSet rs = pstmt.executequery (sqlString);   Executes the SQL statement that queries the database, returning a result set (ResultSet) object.   int rows = pstmt.executeupdate (sqlString);   Used to perform INSERT, UPDATE, or DELETE statements, and so on.   (5) Processing result set while (Rs.next ()) {String name = rs.getstring ("name"); String pass = rs.getstring (1);   This method is more efficient}

JDBC Database connection

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.