Try reading this documentation and example.
EDIT
just modified example from above links
prepared steps: - download MySQL Server - download mySQL java driver - download Apache Commons Pool - download Commons DBCP - Open a MySQL client, for example MySQL Workbench, and create a database using the following script
delimiter $$ CREATE DATABASE `test_stackoverflow` $$ delimiter $$ CREATE TABLE `test_table` ( `idtest_table` int(11) NOT NULL, `test_field` varchar(45) DEFAULT NULL, PRIMARY KEY (`idtest_table`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8$$ INSERT INTO `test_stackoverflow`.`test_table` (`idtest_table`, `test_field`) VALUES (1, 'test1'); INSERT INTO `test_stackoverflow`.`test_table` (`idtest_table`, `test_field`) VALUES (2, 'test2');
- create a java project, add the myscl connector, pool and dbcp to the class path (you just load all these banks)
add the following classes
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS; import org.apache.commons.dbcp.datasources.SharedPoolDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class Pool { private static DataSource ds; static { DriverAdapterCPDS cpds = new DriverAdapterCPDS(); try { cpds.setDriver("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace();
username and password must be changed to your db user / password
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MainClass { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = Pool.getConnection();
Just run the main method and you will see the printed test values ββfor the console !!!
source share