Linux system Java connection MySQL database

Source: Internet
Author: User

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:

  1. Import java.sql.*;

  2. public class Jdbctest {

  3. public static void Main (string[] args) {

  4. Driver name

  5. String Driver = "Com.mysql.jdbc.Driver";

  6. The URL points to the database name you want to access Scutcs

  7. String url = "Jdbc:mysql://127.0.0.1:3306/student";

  8. User name when MySQL is configured

  9. String user = "root";   

  10. Password for MySQL configuration

  11. String password = "";

  12. try {

  13. Load Driver

  14. Class.forName (driver);

  15. Connecting to a database

  16. Connection conn = drivermanager.getconnection (Url,user,password);

  17. if (!conn.isclosed ())

  18. SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");

  19. statement used to execute SQL statements

  20. Statement Statement = Conn.createstatement ();

  21. The SQL statement to execute

  22. String sql = "SELECT * FROM Std";

  23. Result set

  24. ResultSet rs = statement.executequery (SQL);

  25. System.out.println ("-----------------");

  26. System.out.println ("Execution results are as follows:");

  27. System.out.println ("-----------------");

  28. System.out.println ("School number" + "\ T" + "name");

  29. System.out.println ("-----------------");

  30. String name = NULL;

  31. while (Rs.next ()) {

  32. Select sname This column of data

  33. Name = rs.getstring ("name");   

  34. 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.

  35. The specified byte array is then decoded using the GB2312 character set

  36. name = new String (name.getbytes ("iso-8859-1"), "UTF8");

  37. Output results

  38. System.out.println (rs.getstring ("id") + "\ T" + name);

  39. }

  40. Rs.close ();

  41. Conn.close ();

  42. } catch (ClassNotFoundException e) {

  43. System.out.println ("Sorry,can ' t find the driver!");

  44. E.printstacktrace ();

  45. } catch (SQLException e) {

  46. E.printstacktrace ();

  47. } catch (Exception e) {

  48. E.printstacktrace ();

  49. }

  50. }

  51. }

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

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.