Go Java connection to MySQL database using JDBC method and example "graphic description"

Source: Internet
Author: User
Tags stmt

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 to build more advanced tools and interfaces that enable database developers to write database applications.

If you want to use the database to add the driver of the database, different databases have not the driver, here is not explained, the method of adding Jar program driver package is not explained here,

Another article contains an introduction to Http://www.cnblogs.com/taoweiji/archive/2012/12/11/2812295.html

Here is an example of the MySQL database connection, the other database method is similar.

/**
* @author: Tao Weiki, Weibo: Http://weibo.com/taoandtao
* @date: 2012/12/11
* @place: China soft 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;
MySQL jdbc url writing method: jdbc:mysql://host Name: The name of the connection port/database? parameter = value
Avoid Chinese garbled to specify Useunicode and characterencoding
To create a database on a database management system before performing a database operation, the name will be
The following statement is preceded by the creation of the Javademo database
String url = "Jdbc:mysql://localhost:3306/javademo?"
+ "User=root&password=root&useunicode=true&characterencoding=utf8";

try {
The reason to use the following statement is because you want to use the MySQL driver, so we have to drive it up,
It can be loaded through class.forname, or it can be driven by initialization, and the following three forms can be
Class.forName ("Com.mysql.jdbc.Driver");//dynamic load MySQL Driver
Or
Com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();
Or
New Com.mysql.jdbc.Driver ();

SYSTEM.OUT.PRINTLN ("Load MySQL driver successfully");
A connection represents a database connection
conn = drivermanager.getconnection (URL);
Statement contains many methods, such as executeupdate can be inserted, update and delete, etc.
Statement stmt = Conn.createstatement ();
sql = "CREATE TABLE student (no char", name varchar (), primary key (No)) ";
int result = stmt.executeupdate (SQL);//The Executeupdate statement returns an affected number of rows if 1 is not successful
if (Result! =-1) {
SYSTEM.OUT.PRINTLN ("Create data Table Success");
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 a null value is returned
System.out.println ("School number \ t name");
while (Rs.next ()) {
System.out
. println (rs.getstring (1) + "\ T" + rs.getstring (2));//Enter if the type of int is returned can be used with getint ()
}
}
} catch (SQLException e) {
System.out.println ("MySQL operation Error");
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
Conn.close ();
}

}

}

Transferred from: http://www.cnblogs.com/taoweiji/archive/2012/12/11/2812852.html

[goto]java method and instance of using JDBC to connect to a MySQL database] "description"

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.