/*
* Archivos.java
*
* Created on 8 de julio de 2008, 01:34 PM
*/
package calculadora;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Owner
*/
public class Archivos extends javax.swing.JFrame {
/** Creates new form Archivos */
public Archivos() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
////GEN-BEGIN:initComponents //GEN-END:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
btn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
area = new javax.swing.JTextArea();
btng = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
btn.setText("BTN");
btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnActionPerformed(evt);
}
});
area.setColumns(20);
area.setRows(5);
jScrollPane1.setViewportView(area);
btng.setText("BTN2");
btng.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btngActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jScrollPane1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btng, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))))
.addContainerGap(21, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(btn)
.addGap(18, 18, 18)
.addComponent(btng)
.addContainerGap(50, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}//
private void btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActionPerformed
area.setText("");
JFileChooser fileChooser = new JFileChooser();
int seleccion=fileChooser.showOpenDialog(area);
if (seleccion == JFileChooser.APPROVE_OPTION){
try {
File fichero = fileChooser.getSelectedFile();
BufferedReader reader = new BufferedReader(new FileReader(fichero));
String linea="";
linea = reader.readLine();
while (linea != null){
try {
area.append(linea+"\n");
linea = reader.readLine();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"caja linea");
}
}
}catch(IOException ex) {
JOptionPane.showMessageDialog(null,"no lee linea");
}
}
if(seleccion==JFileChooser.CANCEL_OPTION){
}
// TODO add your handling code here:
}//GEN-LAST:event_btnActionPerformed
private void btngActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btngActionPerformed
String doc=".doc";
JFileChooser fileChooser = new JFileChooser();
int seleccion = fileChooser.showSaveDialog(area);
if (seleccion == JFileChooser.APPROVE_OPTION)
{
File fichero = fileChooser.getSelectedFile();
try {
fichero.createTempFile("file1", doc, fichero);
} catch (IOException ex) {
Logger.getLogger(Archivos.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter writer;
try {
writer = new PrintWriter(fichero);
writer.format(doc, area);
writer.print(area.getText());
writer.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"erro");
}
}
if(seleccion==fileChooser.CANCEL_OPTION){}
// TODO add your handling code here:
}//GEN-LAST:event_btngActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Archivos().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea area;
private javax.swing.JButton btn;
private javax.swing.JButton btng;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}
MARI
Showing posts with label Java Intermedio. Show all posts
Showing posts with label Java Intermedio. Show all posts
Monday, February 23, 2009
abrir y crear archivos planos
codigo generado con el ide netbeans 6.1
Thursday, February 19, 2009
Conexion Base de Datos en MySQL
Bueno estq es la clase que se utiliza para conectar una base de datos creado en MySQL (Como dato la base ya debe de estar creada)
package Control;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.sql.DataSource;
public class DataBase {
public Connection con = null;
public Connection getConexion() throws SQLException{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/nombre de la base de datos";//en caso de q la base sea local
// String url = "jdbc:mysql://"direccion ip del servidor": "puerto de MySql"/"nombre de la base de datos""; // en caso de ser cliente servidor
String user = "root";// es el usuario default de MySQL
String password="password de MySQL";
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,"¡No se encontro el driver jdbc instalado en el sistema!\n\n"+ex.getMessage(),"Error de conexion",JOptionPane.ERROR_MESSAGE);
}
return con;
}
public static void main(String []arg){
DataBase k = new DataBase();
try{
JOptionPane.showMessageDialog(null,k.getConexion());
}catch(SQLException ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
Subscribe to:
Posts (Atom)
