JDBC Driver connectivity test with MySQL
[ root @ dbversity : /var/lib/mysql ] tar -zxvf mysql-connector-java-5.1.36.tar.gz
mysql-connector-java-5.1.36/
mysql-connector-java-5.1.36/docs/
–blah blah —–
mysql-connector-java-5.1.36/src/testsuite/ssl-test-certs/server-cert.pem
mysql-connector-java-5.1.36/src/testsuite/ssl-test-certs/server-key.pem
mysql-connector-java-5.1.36/src/testsuite/ssl-test-certs/server-req.pem
mysql-connector-java-5.1.36/src/testsuite/ssl-test-certs/test-cert-store
[ root @ dbversity : /var/lib/mysql ]
[ root @ dbversity : /var/lib/mysql ]
[ root @ dbversity : /var/lib/mysql ] cd mysql-connector-java-5.1.36
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] ll -lhrt
total 1.4M
-rw-r–r– 1 root root 18K Jun 19 15:26 COPYING
-rw-r–r– 1 root root 230K Jun 19 15:26 CHANGES
-rw-r–r– 1 root root 89K Jun 19 15:26 build.xml
drwxr-xr-x 8 root root 4.0K Jun 19 15:26 src
-rw-r–r– 1 root root 63K Jun 19 15:26 README.txt
-rw-r–r– 1 root root 60K Jun 19 15:26 README
-rw-r–r– 1 root root 950K Jun 19 15:26 mysql-connector-java-5.1.36-bin.jar
drwxr-xr-x 2 root root 4.0K Aug 26 07:08 docs
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
mysql>
mysql> create user myusr identified by ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to myusr;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql>
mysql>
mysql>
mysql> create database jdbc_db;
mysql> use jdbc_db
Database changed
mysql>
mysql> create table mytbl (id int, name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into mytbl (id,name) values (1,’jdbc driver test’);
Query OK, 1 row affected (0.01 sec)
mysql> insert into mytbl (id,name) values (2,’jdbc driver success’);
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytbl (id,name) values (3,’jdbc connectivity’);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql>
mysql> select * from mytbl;
+——+———————+
| id | name |
+——+———————+
| 1 | jdbc driver test |
| 2 | jdbc driver success |
| 3 | jdbc connectivity |
+——+———————+
3 rows in set (0.00 sec)
mysql>
mysql>
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] cat DBDemo.java
import java.sql.*;
import java.util.Properties;
public class DBDemo
{
private static final String dbClassName = “com.mysql.jdbc.Driver”;
private static final String CONNECTION =
“jdbc:mysql://dbversity/jdbc_db”;
public static void main(String[] args) throws
ClassNotFoundException,SQLException
{
System.out.println(dbClassName);
Class.forName(dbClassName);
Properties p = new Properties();
p.put(“user”,”myusr”);
p.put(“password”,”mypwd”);
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println(“It works !”);
String url = “jdbc:mysql://dbversity:3306/jdbc_db”;
String username = “myusr”;
String password = “mypwd”;
System.out.println(“Connecting database…”);
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println(“Database connected!”);
} catch (SQLException e) {
throw new IllegalStateException(“Cannot connect the database!”, e);
}
Connection conn = DriverManager.getConnection
(“jdbc:mysql://dbversity:3306/jdbc_db”, “myusr”, “mypwd”);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT id,name FROM mytbl”);
System.out.printf(“%n”);
System.out.println(“id name”);
while (rs.next()) {
int x = rs.getInt(“id”);
String s = rs.getString(“name”);
System.out.println(x + ” ” + s);
}
stmt.close();
conn.close();
c.close();
}
}
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] javac DBDemo.java
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] ll -lhrt
total 1.5M
-rw-r–r– 1 root root 18K Jun 19 15:26 COPYING
-rw-r–r– 1 root root 230K Jun 19 15:26 CHANGES
-rw-r–r– 1 root root 89K Jun 19 15:26 build.xml
drwxr-xr-x 8 root root 4.0K Jun 19 15:26 src
-rw-r–r– 1 root root 63K Jun 19 15:26 README.txt
-rw-r–r– 1 root root 60K Jun 19 15:26 README
-rw-r–r– 1 root root 950K Jun 19 15:26 mysql-connector-java-5.1.36-bin.jar
drwxr-xr-x 2 root root 4.0K Aug 26 07:08 docs
-rw-r–r– 1 root root 683 Aug 26 09:08 DBDemo.java_bkp
-rw-r–r– 1 root root 1.5K Aug 26 09:33 DBDemo.java
-rw-r–r– 1 root root 2.8K Aug 26 09:37 DBDemo.class
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ] java -cp .:/var/lib/mysql/mysql-connector-java-5.1.36/mysql-connector-java-5.1.36-bin.jar DBDemo
com.mysql.jdbc.Driver
It works !
Connecting database…
Database connected!
id name
1 jdbc driver test
2 jdbc driver success
3 jdbc connectivity
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36 ]
[ root @ dbversity : /var/lib/mysql/mysql-connector-java-5.1.36