/*
* form11.java
*
* Created on October 8, 2016, 1:28 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 form11 extends javax.swing.JFrame {
/** Creates new form form11 */
public form11() {
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() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jTextField1FocusLost(evt);
}
});
jTextField2.setText("jTextField2");
jTextField3.setText("jTextField3");
jTextField4.setText("jTextField4");
jButton1.setText("find");
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(102, 102, 102)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
.addContainerGap(153, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19)
.addComponent(jButton1)
.addContainerGap(107, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1FocusLost(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 Fname,Lname,Address,Salary from emp";
Statement statement = con.createStatement();
//rs=statement.execute(sql);
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
String vfirstName = rs.getString("Fname");
String vlastName = rs.getString("Lname");
String vAddress = rs.getString("Address");
int vSalary = rs.getInt("Salary");
// Date dateCreated = rs.getDate("date_created");
// boolean isAdmin = rs.getBoolean("is_admin");
//int numPoints = rs.getInt("num_points");
//int id = rs.getInt("id");
// print the results
//System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints);
jTextField1.setText(vfirstName);
jTextField2.setText(vlastName);
jTextField3.setText(vAddress);
jTextField4.setText(vAddress);
}
}
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 = "select Fname,Lname,Address,Salary from emp";
Statement statement = con.createStatement();
//rs=statement.execute(sql);
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
String vfirstName = rs.getString("Fname");
String vlastName = rs.getString("Lname");
String vAddress = rs.getString("Address");
int vSalary = rs.getInt("Salary");
// Date dateCreated = rs.getDate("date_created");
// boolean isAdmin = rs.getBoolean("is_admin");
//int numPoints = rs.getInt("num_points");
//int id = rs.getInt("id");
// print the results
//System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints);
jTextField1.setText(vfirstName);
jTextField2.setText(vlastName);
jTextField3.setText(vAddress);
jTextField4.setText(vAddress);
}
}
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 form11().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
No comments:
Post a Comment