Advertisement
Guest User

Untitled

a guest
Nov 11th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import com.zaxxer.hikari.HikariConfig;
  9. import com.zaxxer.hikari.HikariDataSource;
  10.  
  11. public class DataSource {
  12.  
  13.     private static HikariConfig config = new HikariConfig();
  14.     private static HikariDataSource ds;
  15.  
  16.     static {
  17.         config.setJdbcUrl("jdbc:mysql://localhost:3306/sampletable");
  18.  
  19.         config.setUsername("root");
  20.         config.setPassword("00001");
  21.  
  22.         config.addDataSourceProperty("characterEncoding", "utf8");
  23.         config.addDataSourceProperty("useUnicode", true);
  24.  
  25.         config.setMaximumPoolSize(10);
  26.         config.setConnectionTimeout(5000);
  27.         ds = new HikariDataSource( config );
  28.     }
  29.  
  30.     public static Connection getConnection() throws SQLException {
  31.         return ds.getConnection();
  32.     }
  33.  
  34.     public static void main (String[] args) {
  35.         getName("1");
  36.     }
  37.    
  38.     public static String getName(String id) {
  39.         Connection connection = null;
  40.         PreparedStatement preparedStatement = null;
  41.         ResultSet resultSet = null;
  42.        
  43.         try {
  44.             //Get a connection from the connection pool
  45.             connection = getConnection();
  46.            
  47.             //SQL Query
  48.             String query = "SELECT * FROM TABLE1 WHERE ID = ?";
  49.            
  50.             //Prepare the statement
  51.             preparedStatement = connection.prepareStatement(query);
  52.            
  53.             //Sets the cache size to 0
  54.             preparedStatement.setFetchSize(0);
  55.            
  56.             //Basically defines the question marks in the query
  57.             preparedStatement.setObject(1, id);
  58.            
  59.             //Gets the result
  60.             resultSet = preparedStatement.getResultSet();
  61.            
  62.             //Loops through all the rows returned from the execution, if the ID is completely unique you will only have row (Unless you fucked up).
  63.             while(resultSet.next()) {
  64.                 //In this case it should only have one value (you shouldn't even loop in this case)
  65.                 return resultSet.getString(1);
  66.             }
  67.         }catch(SQLException e) {
  68.             e.printStackTrace();
  69.         }finally{
  70.             try {
  71.                 if(connection != null) connection.close();
  72.                 if(preparedStatement != null) preparedStatement.close();
  73.                 if(resultSet != null) resultSet.close();
  74.             }catch(SQLException e) {
  75.                 e.printStackTrace();
  76.             }
  77.         }
  78.         return null;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement