Jdbc connection example for implementing Mysql in Java _ MySQL

Source: Internet
Author: User
Java implementation of Mysql jdbc connection example bitsCN.com

First, create a database in the MySQL console
SQL code

Create database test;
Use test;
Create table user (username varchar (15), password varchar (20 ));
Insert into user values ('userone', '201312 ');
You can also use MySQL-Front to create

Java code

Package com. dgy. util;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Public class TestJDBC {
/**
* 1. the mysql driver package used is a mysql-connector-java-5.0.8-bin.jar
* 2. The Statement is used to execute a static SQL Statement and return the object of the result it generates.
* By default, only one ResultSet object can be opened for each Statement object at a time.
* Therefore, if you read a ResultSet object and read another object,
* The two objects must be generated by different Statement objects.
* If the current ResultSet object opened by a statement exists,
* All execution methods in the Statement interface are implicitly disabled.
* 3. ResultSet indicates the data table of the database result set, which is usually generated by executing a database query statement.
* The ResultSet object has a pointer to its current data row. Initially, the pointer is placed before the first line.
* The next method moves the pointer to the next row;
* Because this method returns false if there is no next row in the ResultSet object,
* You can use it in the while loop to iterate the result set.
**/
Static Connection conn = null;
Public static Connection getConnectionByJDBC (){
Try {
// Load the driver package
Class. forName (com. mysql. jdbc. Driver "); // load the Driver
} Catch (ClassNotFoundException e ){
System. out. println ("An exception occurred when loading the driver package! Please check it! ");
E. printStackTrace ();
}
Try {
/** Create a jdbc connection, but pay attention to the first parameter of this method,
* If a CommunicationsException exception occurs in 127.0.0.1,
* It may need to be changed to localhost.
**/
// Jdbc: mysql: // localhost: 3306/test, and test is a database
Conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", "123456 ");
} Catch (SQLException e ){
System. out. println ("An exception occurred when connecting to the database! ");
E. printStackTrace ();
}
Return conn;
}
Public static void test (){
String SQL = "select * from user ";
GetConnectionByJDBC ();
Try {
// Create a jdbc statement
Statement stmt = conn. createStatement ();
// Execute the query
ResultSet rs = stmt.exe cuteQuery (SQL );
While (rs. next ()){
String username = rs. getString ("username ");
String password = rs. getString ("password ");
System. out. println (username + "" + password );
}
} Catch (SQLException e ){
System. out. println (e. getMessage ());
E. printStackTrace ();
} Finally {
// Closes the connection proactively (to avoid the connection being closed in the try statement block when an exception occurs)
Try {
If (conn! = Null) conn. close ();
} Catch (SQLException e ){
System. out. println (e. getMessage ());
E. printStackTrace ();
}
}
}
Public static void main (String [] args ){
TestJDBC testjdbc = new TestJDBC ();
Testjdbc. test ();
}
}

Author: "netsky"

BitsCN.com

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.