Access mysql in java

Source: Internet
Author: User
Access mysql using java to quickly build mysql databases. Source: dujid


Environment:

Operating system: RedHat linux 7.1

Mysql server: 3.23.36 (included in linux 7.1)
Mysql jdbc: 2.0.14
Java JDK: j2sdk-1.4.0_02

Obtain mysql jdbc:


Mysql-connector-java-2.0.14.tar.gz

:


1. HKLPG/Hong Kong
2.ihostunit.com/Hong Kong
3. shellhung.org/Hong Kong

Obtain j2sdk:

Java.sun.com's j2sdk-1.4.0_02

Install mysql jdbc:


Shell> tar-zxpf mysql-connector-java-2.0.14.tar.gz

Installing j2sdk is not described here.

Set CLASSPATH:


Shell> CLASSPATH = $ CLASSPATH:/your mysql-jdbc driver Directory
Mysql-connector-java-2.0.14-bin.jar
Shell> export CLASSPATH

Establish a connection in java:


Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection conn = DriverManager. getConnection
("Jdbc: mysql: // localhost/test? User = root & password = ");

Test:

Premise: your CLASSPATH must be correct, and javac and java can run normally.


Shell> cd/directory of your mysql-jdbc driver/testsuite
Shell> java DateTest

If the screen prompts:


About to insert date 1017684000000
Inserted row 1
Selected date from row 1: 1017684000808
1974-01-01
1974-01-01

That is, everything is normal.

Exception:

When DateTest is run, the screen prompts "Server configuration denies access to data source"

Solution:

First enter mysql and log on as root, and then enter the following content.


Mysql> grant all privileges on [dbname]. * to '[user]' @ '[hostname]'
Identified by '[password]';

(Note: Here, replace dbname with your database name, which is test in DateTest; replace user with your user name, which is root by the author; replace the hostname with the machine on which the java program runs. the host runs on the local machine, that is, 'localhost'. replace the password with the password of your mysql User, that is ′′.)


Mysql> FLUSH PRIVILEGES

Please contact me if you have any questions, dujid ronyou@baoji.gov.cn.

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.