right click JtextField1 property to change Name of JtextField to txtFname
/*
* form2.java
*
* Created on October 8, 2016, 12:33 PM
*/
package myproject123;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Administrator
*/
public class form2 extends javax.swing.JFrame {
/** Creates new form form2 */
public form2() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
txtFname = new javax.swing.JTextField();
txtLname = new javax.swing.JTextField();
txtAddress = new javax.swing.JTextField();
txtSalary = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txtLname.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
txtLnameFocusLost(evt);
}
});
jButton1.setText("save insert");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtFname, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addComponent(txtLname)
.addComponent(txtAddress)
.addComponent(txtSalary)))
.addContainerGap(150, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(txtFname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(15, 15, 15)
.addComponent(txtLname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19)
.addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(67, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtLnameFocusLost(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
try
{
//database without DSN ms access
//String database="D:/mydatabasefolder/employee.mdb";//Here database exists in the current directory
// String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";DriverID=22;READONLY=true";
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//con=DriverManager.getConnection(url);
//
//database mysql
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dblogin","root","vp");
String sql = "select * from emp";
Statement statement = con.createStatement();
//rs=statement.execute(sql);
ResultSet rs = statement.executeQuery(sql);
// JOptionPane.showMessageDialog(null, "saved successfully");
//createMessageBox("Inserted Successfully");
txtFname.setText(rs.getString("fName"));
txtLname.setText(rs.getString("lName"));
txtAddress.setText(rs.getString("Address"));
txtSalary.setText(rs.getString("Salary"));
}
catch(Exception e)
{
//createMessageBox(e.getMessage());
}
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try {
//database without DSN ms access
//String database="D:/mydatabasefolder/employee.mdb";//Here database exists in the current directory
// String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";DriverID=22;READONLY=true";
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//con=DriverManager.getConnection(url);
//
//database mysql
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dblogin","root","vp");
String sql = "Insert Into Emp (FName,LName,Address,Salary) " +
"Values ('"+txtFname.getText()+"','"+txtLname.getText()
+"','"+txtAddress.getText()+"','"+txtSalary.getText()+"')";
Statement statement = con.createStatement();
statement.execute(sql);
JOptionPane.showMessageDialog(null, "saved successfully");
//createMessageBox("Inserted Successfully");
} catch(Exception e) {
//createMessageBox(e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField txtAddress;
private javax.swing.JTextField txtFname;
private javax.swing.JTextField txtLname;
private javax.swing.JTextField txtSalary;
// End of variables declaration
}
No comments:
Post a Comment