The Java connection MySQL database method under Linux system is similar to that under Windows, the following steps are as follows:
Download JDBC (driver): mysql-connector-java-5.1.37.tar.gz
Unzip the downloaded packet: TAR-ZXVF mysql-connector-java-5.1.37.tar.gz
After decompression, the Mysql-connector-java-5.1.29-bin.jar is located in the mysql-connector-java-5.1.29 directory.
Configuration after decompression, the Mysql-connector-java-5.1.29-bin.jar is located in the mysql-connector-java-5.1.29 directory. JDBC:[[email protected] mysql-connector-java-5.1.37]# CP MYSQL-CONNECTOR-JAVA-5.1.37-BIN.JAR/USR /lib/jvm/java-1.7.0-openjdk.x86_64//jre/lib/ext/
Java Instance Code:
Import java.sql.*;
public class Jdbctest {
public static void Main (string[] args) {
Driver name
String Driver = "Com.mysql.jdbc.Driver";
The URL points to the database name you want to access Scutcs
String url = "Jdbc:mysql://127.0.0.1:3306/student";
User name when MySQL is configured
String user = "root";
Password for MySQL configuration
String password = "";
try {
Load Driver
Class.forName (driver);
Connecting to a database
Connection conn = drivermanager.getconnection (Url,user,password);
if (!conn.isclosed ())
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
statement used to execute SQL statements
Statement Statement = Conn.createstatement ();
The SQL statement to execute
String sql = "SELECT * FROM Std";
Result set
ResultSet rs = statement.executequery (SQL);
System.out.println ("-----------------");
System.out.println ("Execution results are as follows:");
System.out.println ("-----------------");
System.out.println ("School number" + "\ T" + "name");
System.out.println ("-----------------");
String name = NULL;
while (Rs.next ()) {
Select sname This column of data
Name = rs.getstring ("name");
First, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array.
The specified byte array is then decoded using the GB2312 character set
name = new String (name.getbytes ("iso-8859-1"), "UTF8");
Output results
System.out.println (rs.getstring ("id") + "\ T" + name);
}
Rs.close ();
Conn.close ();
} catch (ClassNotFoundException e) {
System.out.println ("Sorry,can ' t find the driver!");
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Database creation:
[[email protected] ~]# MySQL
mysql> CREATE database student;
Query OK, 1 row Affected (0.00 sec)
Mysql> CREATE TABLE std (ID int (5) primary key NOT NULL, name varchar (a) not null);
Query OK, 0 rows affected (0.04 sec)
mysql> DESC STD;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | Int (5) | NO | PRI | NULL | |
| name | varchar (20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in Set (0.00 sec)
mysql> INSERT INTO STD values (001, ' www '), (002, ' QQQ ');
Query OK, 2 rows Affected (0.00 sec)
Records:2 duplicates:0 warnings:0
mysql> Select *from std;
+----+------+
| ID | name |
+----+------+
| 1 | www |
| 2 | QQQ |
+----+------+
2 rows in Set (0.00 sec)
Test results:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/74/DF/wKiom1YszKfAFUZsAABBVxX9-bk555.jpg "title=" Qq20151025203722.png "alt=" Wkiom1yszkfafuzsaabbvxx9-bk555.jpg "/>
This article is from the "canvas shoes can walk the cat step" blog, please be sure to keep this source http://9409270.blog.51cto.com/9399270/1706102
Linux system Java connection MySQL database