Facebook
From Taylor Smith, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 77
  1. package randomdataset;
  2.  
  3. import java.sql.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. public class InsertMySQL {
  8.  
  9.     private String url = " ";
  10.     private String user = "root";
  11.     private String password = "2916";
  12.     private String filePath;
  13.  
  14.     public InsertMySQL(String url, String filePath) {
  15.         this.url = url;
  16.         this.filePath = filePath;
  17.         //pass database address (url) and text file path (filePath)
  18.     }
  19.  
  20.     public void connect() {
  21.         try (Connection con = DriverManager.getConnection(url, user, password)) {
  22.             Class.forName("");
  23.  
  24.             if (con != null) {
  25.                 System.out.println("Connection to database successful");
  26.             }
  27.         } catch (ClassNotFoundException e) {
  28.             System.out.println("Database driver class not found");
  29.             e.printStackTrace();
  30.         } catch (SQLException e) {
  31.             System.out.println("Error with SQL");
  32.             e.printStackTrace();
  33.         }
  34.     }
  35.  
  36.     public void insert() {
  37.         try {
  38.             try (Connection con = DriverManager.getConnection(url, user, password)) {
  39.                 String querySetLimit = "SET GLOBAL max_allowed_packet=104857600;";  // 10 MB
  40.                 Statement stSetLimit = con.createStatement();
  41.                 stSetLimit.execute(querySetLimit);
  42.  
  43.                 String sql = "INSERT INTO patient(ID, sex, age) values (LOAD_FILE(?))";
  44.                 PreparedStatement statement = con.prepareStatement(sql);
  45.  
  46.                 statement.setString(1, filePath);
  47.  
  48.                 int tuple = statement.executeUpdate();
  49.  
  50.                 if (tuple > 0) {
  51.                     System.out.println("A patient was inserted");
  52.                 }
  53.             }
  54.         } catch (SQLException ex) {
  55.             Logger.getLogger(InsertMySQL.class.getName()).log(Level.SEVERE, null, ex);
  56.         }