Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


omkar
java
3 Answer(s)      5 years and 9 months ago
Posted in : Java Beginners



Advertisement
View Answers

April 8, 2009 at 12:10 PM


Hi friend,

To maintain the backup of MySQL,you can use the following code:

import java.util.*;
import java.io.*;

public class BackUpSql {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\ab.bat");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
But first of all, you need to create a bat file 'ab.bat' that contains the following commands:

set path=%path%;C:\Program files\MySQL\MySQL Server 5.0\bin;
mysqldump -uroot -proot test >C:\back.sql

where root is the username and password of our database connection and test is the database name.


April 8, 2009 at 12:13 PM


continue........................

To upload a photo and insert into database,use the following code:

import java.io.*;
import java.util.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;

public class Uploader extends JFrame {
static JFrame frame;
JList list;
DefaultListModel model;
JFileChooser chooser;
File currentDir;
JButton addButton, uploadButton;

public Uploader() {
setSize(300,105);
setResizable(true);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action uploadAction = new UploadAction("Upload",new ImageIcon());
Action browseAction = new BrowseAction("Browse...",new ImageIcon());
JPanel cp = new JPanel();
cp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
cp.setLayout(new BorderLayout());
setContentPane(cp);
model = new DefaultListModel();

list = new JList(model);
list.getActionMap();
list.setVisibleRowCount(12);
list.setCellRenderer(new ImageCellRenderer());
cp.add(new JScrollPane(list),BorderLayout.CENTER);

JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10));

JLabel uploadLabel = new JLabel();
uploadLabel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
uploadButton = new JButton(uploadAction);
uploadLabel.add(uploadButton);
buttons.add(uploadButton);

addButton = new JButton(browseAction);
buttons.add(addButton);
cp.add(buttons,BorderLayout.SOUTH);
currentDir = new File(System.getProperty("user.dir"));
}
public int addToListModel(ArrayList filenames) {
int count = 0;
for ( Iterator i = filenames.iterator(); i.hasNext(); ) {
String filename = (String)i.next();
if (!model.contains(filename)) {
model.addElement(filename);
count++;
}
}
return count;
}
public static void main(String [] args) {
frame = new Uploader();
frame.setVisible(true);

}
class BrowseAction extends AbstractAction {
public BrowseAction(String text,Icon icon) { super(text,icon); }
public void actionPerformed(ActionEvent evt) {
chooser = new JFileChooser(currentDir);
chooser.addChoosableFileFilter(new ImageFileFilter());
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(true);
chooser.setDragEnabled(true);
chooser.showOpenDialog(frame);
currentDir = chooser.getCurrentDirectory();

File [] files = chooser.getSelectedFiles();
ArrayList filenames = new ArrayList(files.length);
for (int i = 0; i < files.length; i++)
filenames.add(files[i].getName());
addToListModel(filenames);
}
}


April 8, 2009 at 12:14 PM


class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory()) return false;
String name = file.getName().toLowerCase();
return (name.endsWith(".jpg") || name.endsWith(".png")|| name.endsWith(".gif"));
}
public String getDescription() { return "Images (*.gif,*.bmp, *.jpg, *.png )"; }
}
class UploadAction extends AbstractAction {
public UploadAction(String text,Icon icon) { super(text,icon); }

public void actionPerformed(ActionEvent evt) {
String upload = chooser.getSelectedFile().getAbsolutePath();
System.out.println("Upload : " + upload);
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";;
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();

connection = DriverManager.getConnection(connectionURL, "root", "root");
File image = new File(upload);
psmnt = connection.prepareStatement("insert into photo( image) "+ "values(?)");
//psmnt.setInt(1,5);

fis = new FileInputStream(image);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded and insert successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
}
catch (Exception ex) {
System.out.println("Found some error : "+ex);
}
finally {
try{
connection.close();
psmnt.close();
}
catch(Exception exc){}
}
}
}
class ImageCellRenderer extends JLabel implements ListCellRenderer {
public ImageCellRenderer() {
setOpaque(true);
setIconTextGap(12);
}
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected, boolean cellHasFocus){
File f = new File(value.toString());
Border empty = BorderFactory.createEmptyBorder(3,3,3,3);
Border matte = BorderFactory.createMatteBorder(0,0,1,0,Color.white);
setBorder(BorderFactory.createCompoundBorder(matte,empty));
setText(f.getName());

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image icon = toolkit.getImage(f.getPath());
Image scaledIcon = icon.getScaledInstance(40,40,Image.SCALE_FAST);
setIcon(new ImageIcon(scaledIcon));
return this;
}
}
}

Thanks


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com