Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

Java में LOAD DATA LOCAL INFILE का उपयोग करना

यहां कार्य कोड है जिसका मैंने परीक्षण किया:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoadTRPLog2MySql {

    public static void main(String[] args) {

        Class driver_class = null;
        try {
            driver_class = Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("found driver" + driver_class);


        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://mysqlserver.com:3306/dbname", "myid","pwd");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("Established connection to " + connection.getMetaData().getURL());
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        Statement statement = null;
        try {
            statement = connection.createStatement();
            Statement statement1 = connection.createStatement();
            //windows
            //statement1.executeUpdate( "LOAD DATA LOCAL INFILE 'C:\\Users\\senthil_sivasamy\\Documents\\Projects\\messageprocessing\\log.txt' INTO TABLE  trpwatchlog_tb FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
            //linux  ( " LOAD DATA LOCAL INFILE '/home/username/logname.log' INTO TABLE  logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
            statement.executeUpdate( "LOAD DATA LOCAL INFILE '/home/username/avail30trplog' INTO TABLE  logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");

            statement1.execute("select * from dbname.tablelog_tb");
            ResultSet rs = statement1.getResultSet();
            System.out.println("Row hostname and timestamp");
            while(rs.next()) {
                System.out.println("Row hostname and timestamp");
                System.out.println(rs.getRow());
                System.out.println(""+rs.getString("hostname"));
                System.out.println(""+rs.getString("timestamp"));
            }
            rs.close();
        } catch(SQLException e) {
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. पीडीओ पीएचपी में पेजिनेशन

  2. MySQL में CONCAT में GROUP_CONCAT का उपयोग कैसे करें

  3. कॉलम के सभी मानों को लोअरकेस में अपडेट करें

  4. MySQL में दिए गए कैरेक्टर के लिए ASCII कोड कैसे खोजें

  5. एकाधिक $_POST सरणियों से MYSQL पंक्ति में कैसे सम्मिलित करें