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.