Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nayaz.patient;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.InputStream;
- import java.awt.event.ActionEvent;
- @SuppressWarnings("serial")
- public class Demo extends JFrame{
- public static final String DATABASE_NAME = "nayaz";
- public static final String DATABASE_SERVER = "localhost";
- public static final String DATABASE_USER_ID = "root";
- public static final String DATABASE_PASSWORD = "";
- public static final String connection_url = "jdbc:mysql://" + DATABASE_SERVER + "/" + DATABASE_NAME;
- Connection connection=null;
- File selectedFile;
- Statement ps=null;
- ResultSet rs=null;
- String filePath=null;
- private JTextField textField;
- public Demo() {
- // TODO Auto-generated constructor stub
- super("Patient Details Form");
- setSize(858, 531);
- getContentPane().setLayout(null);
- JLabel lblNewLabel = new JLabel("Pick a file");
- lblNewLabel.setBounds(10, 115, 151, 32);
- getContentPane().add(lblNewLabel);
- textField = new JTextField();
- textField.setBounds(134, 115, 352, 32);
- getContentPane().add(textField);
- textField.setColumns(10);
- JButton btnNewButton = new JButton("Browse");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try
- {
- JFileChooser fileChooser = new JFileChooser(new File("C:\"));
- fileChooser.setMultiSelectionEnabled(false);
- fileChooser.setVisible(true);
- int returnValue = fileChooser.showOpenDialog(null);
- if (returnValue == JFileChooser.APPROVE_OPTION) {
- selectedFile = fileChooser.getSelectedFile();
- // selectedFile.getAbsolutePath();
- if(selectedFile!=null){filePath=selectedFile.getPath();
- System.out.println(filePath);
- }
- if(filePath!=null){
- textField.setText("File:"+" "+filePath);
- InputStream input = getClass().getResourceAsStream(filePath);
- System.out.println(input);
- }
- System.out.println(selectedFile.getName());
- }
- }catch(Exception e1)
- {
- JOptionPane.showInputDialog(this, e1.getMessage());
- e1.printStackTrace();
- }
- }
- });
- btnNewButton.setBounds(195, 188, 169, 23);
- getContentPane().add(btnNewButton);
- JButton btnNewButton_1 = new JButton("Load");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if(filePath!=null )
- {
- try {
- Connection connection = DriverManager.getConnection(connection_url, DATABASE_USER_ID, DATABASE_PASSWORD);
- ps = connection.createStatement();
- String query = "LOAD DATA LOCAL INFILE " + filePath + " patient1 "
- + "FIELDS TERMINATED BY ',' " +
- "ENCLOSED BY '//' " +
- "LINES TERMINATED BY 'rn' ";
- int val = 0;
- val = ps.executeUpdate(query);
- if(val>=1)JOptionPane.showInputDialog(this, "Succesfully Stored...");
- else
- JOptionPane.showInputDialog(this, "Error in storage...");
- }
- catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- btnNewButton_1.setBounds(396, 188, 137, 23);
- getContentPane().add(btnNewButton_1);
- }
- public static void main(String args[]) {
- Demo sfc = new Demo();
- sfc.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement