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’,’123456’);
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 = new testjdbc ();
Testjdbc. Test ();
}
}