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




Have Programming Question? Ask it here!
 
 
 


omkar
java
3 Answer(s)      5 years and 5 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  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
Java
Java   Whether Java is pure object oriented Language
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
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   What is ?static? keyword
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
java
java  Does java allows multiline comments
Java
Java  how to do java in command prompt
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance 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  why to set classpath in java
java
java  Write a java code to print "ABABBABCABABBA
java
java  write a program in java to acess the email
java
java  send me java interview questions
java
java  how use java method
java
java  what are JAVA applications development tools
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  is java purely object oriented language
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java   difference between class and interface
Java
Java  how to draw class diagrams in java
java
java  write a java program using filenotfoundexception
java
java pattern code for a given words  java pattern code for a given words pattern
java
java  create java program for delete and update the details,without using database, just a normal java program
java
java code to search the nodes  how to write the java code to search the nodes using routers
java
java  different between java & core java print("code sample
java
java  hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
java
java   How to set java Policy for applet using jdk 6
java
java  dear, i want a field for date picker using java/java script
java
java  why methods in java raise exceptions   Have a look at the following link: Java Exceptions
java
java online telephone directory  i need coding for online telephone directory..by using java....pls help me
java
java  how to edit text document by using java then how to edit starting and ending of text document by using java
java
java  different between java & core java print("code sample
java
java  how can use sleep in java   which book learn of java language
java
java  how to prepare the java   Hi Friend, If you want to learn how to install java, creating and running a java program then go through the following links: http://www.roseindia.net/java/beginners/index.shtml http
java
java  java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java programs
java
java  what is the need of java if java is not there what will happen... work unless you have Java installed, and more are created every day. Java... to scientific supercomputers, cell phones to the Internet, Java is everywhere! http
java
java  1.what type of inheritance supported in java. 2.what is the advantage of java over C++.   Hi Friend, Java supports following... inheritance Advantages of Java over C++: 1)Java is pure object oriented
Advertisements
 

 

 

DMCA.com