Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
inserting a file into a mysql database : need help please Posted by Seamo on 7 Dec 2004 at 1:01 PM
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();


}
}
Report
Re: inserting a file into a mysql database : need help please Posted by xstreamke on 13 Feb 2005 at 7:47 AM
Do you mean that you add records from a file to mysql (U use just an insert into statement) or do you mean something like ms calls a blob? I do not know if you can insert blob's in a mysql server.

Regards,

Kevin

: 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();
:
:
: }
: }
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.