Java Desktop Application Swing and
AWT(Abstract Windows Toolkits)
import
java.sql.*;
import
java.lang.*;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
javax.swing.table.DefaultTableModel;
import
javax.swing.JOptionPane;
public class
testapp extends javax.swing.JFrame {
public testapp() {
initComponents();
bindGridview();
}
Main Class
public class
MainClass
{
//change your mysql database connection
here
// public String
StrUrl="jdbc:mysql://localhost/p2p_library";
public String
StrUrl="jdbc:mysql://localhost:3306/p2p_library";
public String StrUid="root";
public String StrPwd= "";
public static String StrUser;
}
Save
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user= jTextField1.getText();
String password=jTextField2.getText();
String type=jTextField3.getText();
try
{
if
(jTextField1.getText().trim().length()==0 ) {
JOptionPane.showMessageDialog(null,"Enter user name.");
return;
}
if (jTextField2.getText().trim().length()==0
) {
JOptionPane.showMessageDialog(null,"Enter password.");
return;
}
if
(jTextField3.getText().trim().length()==0 ) {
JOptionPane.showMessageDialog(null,"Enter Type.");
return;
}
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/p2p_library","root","");
//jdbc:mysql://localhost:3306/mysql [root on
Default schema]
//jdbc:sqlserver://DATACRAFT-PC\DATACRAFT
[sa on sys]
// Connection
con=DriverManager.getConnection("jdbc:sqlserver://IMS","sa","deer");
//
MainClass mc=new MainClass();
// Connection con;
//
con=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
// Statement stmt=con.createStatement();
// String Query= "Insert into lib_user
values('"+ user+"','"+password+"','"+
type+"')";
// stmt.executeUpdate(Query);
// bindGridview();
String Query= "INSERT INTO lib_user
(user_name,user_password,User_type)VALUES(?,?,?)";
PreparedStatement pstmt =
con.prepareStatement(Query);
pstmt.setString(1, user);
pstmt.setString(2, password);
pstmt.setString(3, type);
pstmt.executeUpdate();
bindGridview();
clearTextBox();
}
catch(Exception e)
{
System.out.println("Error");
}
}
JTable Binding
public void
bindGridview()
{
Connection connection;
try {
MainClass mc=new MainClass();
connection=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
ResultSet rs;
PreparedStatement
stmt=connection.prepareStatement("select Id, user_name, user_password,
user_type from lib_user");
rs = stmt.executeQuery();
jTable1.getColumnModel().getColumn(0).setHeaderValue("Id");
jTable1.getColumnModel().getColumn(1).setHeaderValue("user_name");
jTable1.getColumnModel().getColumn(2).setHeaderValue("user_password");
jTable1.getColumnModel().getColumn(3).setHeaderValue("user_type");
jTable1.getTableHeader().resizeAndRepaint();
// Removing Previous Data
while (jTable1.getRowCount() >
0) {
((DefaultTableModel)
jTable1.getModel()).removeRow(0);
}
//Creating Object []rowData for
jTable's Table Model
int columns = rs.getMetaData().getColumnCount();
while (rs.next()) {
Object[] row = new
Object[columns];
for (int i = 1; i <=
columns; i++) {
row[i - 1] =
rs.getObject(i); // 1
}
((DefaultTableModel)
jTable1.getModel()).insertRow(rs.getRow() - 1,row);
}
} catch (Exception e) {
System.err.println(e);
//System.exit(1);
}
finally {
}
}
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
bindGridview();
}
Select
private void
jTable1MouseClicked(java.awt.event.MouseEvent evt) {
Connection connection;
int row=jTable1.getSelectedRow();
try {
if(row>0)
{
MainClass mc=new MainClass();
connection=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
String
Table_Click=(jTable1.getModel().getValueAt(row,0).toString());
String Sql="select user_name,
user_password, user_type from lib_user where Id='"+Table_Click+"'";
PreparedStatement
stmt=connection.prepareStatement(Sql);
ResultSet rs =
stmt.executeQuery();
if(rs.next())
{
String
name=rs.getString("user_name");
String password=rs.getString("user_password");
String
type=rs.getString("user_type");
jTextField1.setText(name);
jTextField2.setText(password);
jTextField3.setText(type);
}
}
else
{
JOptionPane.showMessageDialog(null,"Please select the table any
rows.");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
Update
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String user= jTextField1.getText();
String password=jTextField2.getText();
String type=jTextField3.getText();
int row=jTable1.getSelectedRow();
Connection connection;
try {
if(row>0)
{
MainClass mc=new MainClass();
connection=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
String
userId=(jTable1.getModel().getValueAt(row,0).toString());
String Sql="update lib_user set
user_name='"+user+"',user_password='"+password+"',
user_type='"+type+"' where Id='"+userId+"'";
PreparedStatement
stmt=connection.prepareStatement(Sql);
stmt.executeUpdate();
bindGridview();
clearTextBox();
}
else
{
JOptionPane.showMessageDialog(null,"Please select the table any
rows.");
}
}
catch(Exception ex)
{
System.err.println(ex);
}
}
Edit
private void
jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int row=jTable1.getSelectedRow();
Connection connection;
try {
if(row>0)
{
MainClass mc=new MainClass();
connection=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
// int row=jTable1.getSelectedRow();
String
Table_Click=(jTable1.getModel().getValueAt(row,0).toString());
String Sql="select user_name,
user_password, user_type from lib_user where
Id='"+Table_Click+"'";
PreparedStatement
stmt=connection.prepareStatement(Sql);
ResultSet rs =
stmt.executeQuery();
if(rs.next())
{
String
name=rs.getString("user_name");
String
password=rs.getString("user_password");
String
type=rs.getString("user_type");
jTextField1.setText(name);
jTextField2.setText(password);
jTextField3.setText(type);
}
}
else
{
JOptionPane.showMessageDialog(null,"Please select the table any
rows.");
}
}
catch(Exception ex)
{
// JOptionPane.ShowMessageDialog(null,e);
System.err.println(ex);
}
}
Delete and Clear Textbox
private void
jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
Connection connection;
int row=jTable1.getSelectedRow();
try {
if(row>0)
{
MainClass mc=new MainClass();
connection=DriverManager.getConnection(mc.StrUrl,mc.StrUid,mc.StrPwd);
String userId=(jTable1.getModel().getValueAt(row,0).toString());
String Sql="delete from
lib_user where Id='"+userId+"'";
PreparedStatement
stmt=connection.prepareStatement(Sql);
stmt.executeUpdate();
bindGridview();
}
else
{
JOptionPane.showMessageDialog(null,"Please select the table any
rows.");
}
}
catch(Exception ex)
{
System.err.println(ex);
}
}
public void clearTextBox()
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
No comments:
Post a Comment