I am trying to submit files into a database but I have hit a brich wall. I am using a jfilechooser to get the file and the other infromation will be there from login when up and running. But I don't know how to transmit the file through file i/o with mysql . If any one could give me a hand I would be really grateful. I'll show some of the relevent code to this part of the project.
Regards,
Seamo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.io.*;
public class Submit7 extends JFrame{
private JTextField studentID;
private Connection connection;
private JButton open, submit, saveButton;
private JTextArea info;
JFileChooser fc;
private String fileName;
static private final String newline = "\n";
public Submit7()
{
//connect to the driver to connect to the database.
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch (Exception e){
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void buildGUI()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
//Create a text area for the submission to be acknowledged look at code
JTextArea info = new JTextArea(5,20);
info.setMargin(new Insets(5,5,5,5));
info.setEditable(false);
JScrollPane infoScrollPane = new JScrollPane(info);
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
saveButton = new JButton("submit a file");
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveFile();
}
});
JPanel first = new JPanel();
//Add the text area for info on submissions
first.add(infoScrollPane);
//Add the text field so users can submit their user id
studentID = new JTextField(8);
first.add(studentID);
JPanel second = new JPanel();
second.add(saveButton);
//add panels to container
c.add(first);
c.add(second);
setSize(500,500);
show();
}
//create the method to display errors
private void displaySQLErrors(SQLException e)
{
info.append("SQLException: " + e.getMessage() + "\n");
info.append("SQLState: " + e.getSQLState() + "\n");
info.append("VendorError: " + e.getErrorCode() + "\n");
}
private void saveFile()
{
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
OutputStream textStream =
//This is where a real application would save the file.
info.append("Saving: " + file.getName() + "." + newline);
} else {
info.append("Save command cancelled by user." + newline);
}
//info.setCaretPosition(info.getDocument().getLength());
}
//After the driver has ben accessed the jdbc has to connect to the database
public void connectToDB()
{
try
{
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/submissions?user=root&password=football");
//may have to load in a username and password
//code "?user=spider&password=spider"
}catch(SQLException connectException)
{
System.out.println("Unable to connect to db");
System.exit(1);
}
}
private void init()
{
connectToDB();
}
public static void main(String[] args)
{
Submit7 first = new Submit7();
first.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
first.init();
first.buildGUI();
}
}