MySql sqlTest
public class sqlTest {
private static QueryRunner runner = new QueryRunner( ) ;
private static HikariDataSource dataSource;
static{
HikariConfig config = new HikariConfig( ) ;
config.setDriverClassName( "com.mysql.jdbc.Driver" ) ;
config.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/zetag_server_db?autoReconnect=true" ) ;
config.setUsername( "root" ) ;
config.setPassword( "root" ) ;
config.setAutoCommit( true) ;
config.setMaximumPoolSize( 2 ) ;
config.addDataSourceProperty( "cachePrepStmts" , "true" ) ;
config.addDataSourceProperty( "prepStmtCacheSize" , "250" ) ;
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" ) ;
config.addDataSourceProperty( "useServerPrepStmts" , "true" ) ;
config.addDataSourceProperty( "useServerPrepStmts" , "true" ) ;
config.addDataSourceProperty( "autoReconnect" , "true" ) ;
dataSource = new HikariDataSource( config) ;
}
public static void execute( String sql) {
try {
Connection conn = dataSource.getConnection( ) ;
runner.execute( conn, sql) ;
conn.close( ) ;
} catch( Exception e) {
throw new RuntimeException( e) ;
}
}
public static List< Map< String,Object>> queryBySql( String sql) {
List< Map< String,Object>> result = null;
try {
Connection conn = dataSource.getConnection( ) ;
result = runner.query( conn,sql, new MapListHandler( )) ;
conn.close( ) ;
} catch( Exception e) {
throw new RuntimeException( e) ;
}
return result;
}
public static void main( String[ ] args) {
sqlTest.execute( "sql" ) ;
sqlTest.queryBySql( "sql" ) ;
}
}