MySQL JDBC connection steps and Common parameters _mysql

Source: Internet
Author: User
Tags benchmark stmt

MySQL database from needless to say, now a lot of application development applications and webmasters are often used to, JDBC may generally be more unfamiliar, what does JDBC mean? Also mentioned here, the most important thing is to introduce to you, MySQL JDBC connection steps and common parameters detailed.
  
What does JDBC mean?
  
JDBC (Java Data Base Connectivity,java database connection) is a JAVAAPI for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark by which you can build more advanced tools and interfaces so that database developers can write database applications, and with JDBC, it is easy to send SQL statements to various relational data, Programmers can send SQL calls to the appropriate database by simply writing a program with JDBCAPI.
  
How does JDBC connect to the MySQL database?
  
⑴, the registration JDBC driver needs to be loaded first:
  
Class.forName ("Com.mysql.jdbc.driver");
Class.forName ("Com.mysql.jdbc.driver"). newinstance ();
  
⑵, defining the connection between the driver and the data source Jdbcurl:

⑶, establishes the data Call API Connection object, then establishes the SQL statement object (statementobject), and finally executes the SQL statement:

MySQL JDBC Connection common parameters and detailed explanation:

JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark by which you can build more advanced tools and interfaces that enable database developers to write database applications.

If you want to use the database to add a database driver, different databases have no need to drive, here is not one of the instructions, add Jar program driver package method is not here to explain, another article contains an introduction http://www.jb51.net/article/72672.htm

The following is an example to introduce the MySQL database connection, the other database methods are similar.

/** * @author: Tao Weiki, Weibo: Http://weibo.com/taoandtao * @date: 2012/12/11 * @place: Guangzhou University hua Soft Software Institute/import Java.sql.Driv 
Ermanager; 
Import Java.sql.ResultSet; 
Import java.sql.SQLException; 
Import java.sql.Connection; 
 
 
Import java.sql.Statement; 
  public class Mysqldemo {public static void main (string[] args) throws Exception {Connection conn = null; 
  String SQL; MySQL jdbc URL write way: jdbc:mysql://host Name: Connection port/database name? parameter = value//Avoid Chinese garbled to specify Useunicode and characterencoding//Perform database operations before database
    On the system to create a database, the first name set,///The following statement must first create the Javademo database String url = "Jdbc:mysql://localhost:3306/javademo?" 
 
  + "User=root&password=root&useunicode=true&characterencoding=utf8"; try {//The following statement is used to use the MySQL driver, so we need to drive it,//we can load it through class.forname, or it can be driven by initialization, and the following three forms can be class.for  
   Name ("Com.mysql.jdbc.Driver");//dynamically load MySQL driver//or://com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver (); 
 
   OR://new Com.mysql.jdbc.Driver (); SysTem.out.println ("Successfully loaded MySQL driver"); 
   A connection represents a database connection conn = drivermanager.getconnection (URL); 
   Statement contains many methods, such as executeupdate can be inserted, updated and deleted Statement stmt = Conn.createstatement (); 
   sql = "CREATE TABLE student (no char (), name varchar (), primary key (No))"; int result = stmt.executeupdate (SQL);//Executeupdate statement returns an affected number of rows, if return-1 There is no success if (Result!=-1) {System.out.pri 
    NTLN ("Create data table succeeded"); 
    sql = "INSERT into student (No,name) VALUES (' 2012001 ', ' Tao Weiki ')"; 
    result = Stmt.executeupdate (SQL); 
    sql = "INSERT into student (No,name) VALUES (' 2012002 ', ' Zhou Xiaojun ')"; 
    result = Stmt.executeupdate (SQL); 
    sql = "SELECT * from student"; 
    ResultSet rs = stmt.executequery (sql);//ExecuteQuery Returns a collection of results, otherwise null System.out.println ("number \ t name");  
    while (Rs.next ()) {System.out. println (rs.getstring (1) + "T" + rs.getstring (2));/Enter if the type of int returned can be getint () 
   catch (SQLException e) {System.out.println ("MySQL operation Error"); E. Printstacktrace (); 
  catch (Exception e) {e.printstacktrace (); 
  finally {conn.close ();  } 
 } 
}
Related Article

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.