Mysqljdbc connection steps and common parameters _ MySQL

Source: Internet
Author: User
This article mainly introduces the connection steps and common parameters of mysqljdbc. For more information, see mysql database, jdbc may be unfamiliar. what does jdbc mean? We will also mention that the most important thing is to introduce the mysql jdbc connection steps and common parameters.
  
What does jdbc mean?
  
Jdbc (java data base connectivity, java database connection) is a java API used to execute SQL statements. it can provide unified access to multiple relational databases, it consists of a group of classes and interfaces written in java. Jdbc provides a benchmark to build more advanced tools and interfaces so that database developers can write database applications. with jdbc, it is easy to send SQL statements to various relational data. programmers only need to write a program using jdbcapi to send SQL calls to the corresponding database.
  
How does jdbc connect to the mysql database?
  
(1) you must first load and register the jdbc driver:
  
Class. forname ("com. mysql. jdbc. driver ");
Class. forname ("com. mysql. jdbc. driver"). newinstance ();
  
(2) define the connection between the driver and the data source jdbcurl:

(3) Create a Data Call api connection object, create an SQL declarative object (statementobject), and finally execute the SQL statement:

Common mysql jdbc connection parameters and details:

JDBC (Java Data Base Connectivity, java database connection) is a Java API used to execute SQL statements. it can provide unified access to multiple relational databases, it consists of a group of classes and interfaces written in Java. JDBC provides a benchmark to build more advanced tools and interfaces so that database developers can write database applications.

If you want to use a database, you need to add the database driver. if there are unnecessary drivers for different databases, we will not describe them here. the method for adding the jar program driver package is not described here, another article introduces http://www.bitsCN.com/article/72672.htm.

The following is an example to introduce the connection to the mysql database. the methods for other databases are similar.

/*** @ Author: Tao Weiji, Weibo: http://weibo.com/taoandtao * @ Date: 2012/12/11 * @ place: huasoft software college of Guangzhou University */import java. SQL. driverManager; 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; // JDBC URL of MySQL: jdbc: mysql: // host name: connection port/database name? Parameter = value // avoid Chinese garbled characters. specify useUnicode and characterEncoding. // before performing database operations, create a database on the database management system and set the database name, // Create a javademo database String url = "jdbc: mysql: // localhost: 3306/javademo? "+" User = root & password = root & useUnicode = true & characterEncoding = UTF8 "; try {// The following statement is used because the MySQL driver is used, so we need to drive it, // you can use Class. forName can be loaded in, or driven by Initialization. the following three methods can be used: Class. forName ("com. mysql. jdbc. driver "); // dynamically load the 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); // There are many methods in Statement. for example, executeUpdate can be used to insert, update, and delete Statement stmt = conn. createStatement (); SQL = "create table student (NO char (20), name varchar (20), primary key (NO)"; int result = stmt.exe cuteUpdate (SQL ); // The executeUpdate statement returns an affected number of rows. if-1 is returned, if (result! =-1) {System. out. println ("succeeded in creating a data table"); SQL = "insert into student (NO, name) values ('20170101', 'taowei ki ')"; result = stmt.exe cuteUpdate (SQL); SQL = "insert into student (NO, name) values ('20170101', 'Zhou xiaojun')"; result = stmt.exe cuteUpdate (SQL ); SQL = "select * from student"; ResultSet rs = stmt.exe cuteQuery (SQL); // executeQuery returns a set of results; otherwise, a null value System is returned. out. println ("student ID \ t name"); while (rs. next () {System. out. println (rs. getString (1) + "\ t" + rs. getString (2); // If int type is returned, use getInt () }}catch (SQLException e) {System. out. println ("MySQL operation error"); e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} finally {conn. close ();}}}

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.