Text file insert to sql server advise
843785Sep 9 2008 — edited Sep 10 2008Hi everyone,
Firstly let me just say that I'm not a programmer, and I?m very, very new to this. I've tried my best to find the information out using Google etc, but I'm at the stage were I'm not sure what might help, or what might over complicate things for my very basic level of knowledge.
I work on SQL Server and regularly have to import text files. These text files are always the same layout, but always come in batches, usually 4-5 plus. The file names are different each time, and I've been told that renaming files after they have been sent to us is not an option for any data loader routine.
I've been asked to create a Java data loader with a GUI which allows the user to select a number of files from the one directory. Once the files have been selected they are then loaded to a temporary table in SQL Server. We are currently working on SQL Server 2000.
Again apologies if this is way of the mark but what I've been trying to do is use a JFilechooser to select the files then a prepared statement to try and pass the selected files to SQL Server using a SQL insert.
All this seems to do at the moment is launch the JFilechooser and doesn't actually try to do any of the inserting, I know it because the code is incorrect, but unfortunately because I'm out of my depth on this one I'm not sure where to start! I would appreciate any help.
Thanks:
/*
* DesktopApplication1.java
*/
package desktopapplication1;
import java.sql.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
/**
*
* @author sr
*/
public class testclass extends JFrame
{
private static Connection conn;
String dbupdate1;
private String database;
//opens a gui with a single button which allows you to pick multiple files to load into sequel server
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Dataloader-Java version");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Load a file");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
}
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
//prepared statement to pass the file through to the sequel server db
public void runSql (File f1,File f2)
{
PreparedStatement dbload1;
database = "insert database name here";
String dbupdate1 = "INSERT INTO test (name, status) Values (?, ?)";
try {
openConn();
conn.setCatalog(database);
dbload1 = conn.prepareStatement(dbupdate1);
dbload1.setString(1, f1.getName());
dbload1.setString(2, f2.getName());
dbload1.executeUpdate();
dbload1.close();
System.out.println("Inserted");
closeConn();
} catch (SQLException e) {
System.out.println("Load didn't work correctly, please check: "
+ e.getMessage());
e.printStackTrace();
}
}
//open a connection to sequel server
private void openConn() {
try {
System.out.println("Creating Connection");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://sql1:1433",
"insert database username here", "insert password here");
System.out.println("Connection open");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Open connection exception:" + e.getMessage());
}
}
// closes the connection to sequel server
private void closeConn() {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Close connection exception:" + e.getMessage());
}
}
}