MainClass/src/mainclass/MainClass.java
MainClass/src/mainclass/MainClass.java
package
mainclass
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
javax
.
swing
.
*
;
import
java
.
awt
.
*
;
import
java
.
sql
.
*
;
public
class
MainClass
{
static
JTextField
CustomerName
,
ProductName
,
ProductQuantity
,
CustomerNumber
;
static
JButton
Insert
,
GetProductName
;
private
static
class
Handler
implements
ActionListener
{
@
Override
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
e
.
getActionCommand
()
==
"Insert"
)
{
try
{
Class
.
forName
(
"sun.jdbc.odbc.JdbcOdbcDriver"
);
int
Customernumber
=
Integer
.
parseInt
(
CustomerNumber
.
getText
());
String
Customername
=
CustomerName
.
getText
();
String
Product
=
ProductName
.
getText
();
int
Productquantity
=
Integer
.
parseInt
(
ProductQuantity
.
getText
());
Connection
con
=
DriverManager
.
getConnection
(
"jdbc:mysql://localhost:3306/STORE"
,
"root"
,
"aakash"
);
PreparedStatement
statement
=
con
.
prepareStatement
(
"INSERT INTO PRODUCT VALUES(?,?,?)"
);
statement
.
setInt
(
1
,
Customernumber
);
statement
.
setString
(
2
,
Product
);
statement
.
setInt
(
3
,
Productquantity
);
statement
.
executeUpdate
();
statement
=
con
.
prepareStatement
(
"INSERT INTO CUSTOMER VALUES(?,?)"
);
statement
.
setInt
(
1
,
Customernumber
);
statement
.
setString
(
2
,
Customername
);
statement
.
executeUpdate
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
}
if
(
e
.
getActionCommand
()
==
"GetProductName"
)
{
try
{
Class
.
forName
(
"sun.jdbc.odbc.JdbcOdbcDriver"
);
Connection
con
=
DriverManager
.
getConnection
(
"jdbc:mysql://localhost:3306/STORE"
,
"root"
,
"aakash"
);
Statement
statement
=
con
.
createStatement
();
int
Customernumber
=
Integer
.
parseInt
(
CustomerNumber
.
getText
());
String
quer
=
"SELECT CUSTOMERNAME FROM CUSTOMER WHERE CUSTOMERNUMBER='"
+
Customernumber
+
"'"
;
ResultSet
resultSet
=
statement
.
executeQuery
(
quer
);
if
(
resultSet
.
next
())
{
String
custname
=
resultSet
.
getString
(
1
);
CustomerName
.
setText
(
custname
);
}
String
query
=
"SELECT PRODUCTNAME,QUANTITY FROM PRODUCT WHERE CUSTOMERNUMBER='"
+
Customernumber
+
"'"
;
System
.
out
.
println
(
query
);
resultSet
=
statement
.
executeQuery
(
query
);
if
(
resultSet
.
next
())
{
String
Productname
=
resultSet
.
getString
(
1
);
int
Quantity
=
Integer
.
parseInt
(
resultSet
.
getString
(
2
));
JOptionPane
.
showMessageDialog
(
null
,
"Name of Product="
+
Productname
+
"Quantity="
+
Quantity
,
"Success"
,
JOptionPane
.
PLAIN_MESSAGE
);
}
else
{
JOptionPane
.
showMessageDialog
(
null
,
"Unsuccessful!"
,
"Error"
,
1
);
}
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
}
}
}
MainClass
(){
JFrame
main
=
new
JFrame
(
"MENU FOR STORE"
);
main
.
setBounds
(
350
,
150
,
500
,
500
);
main
.
setVisible
(
true
);
main
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
CustomerNumber
=
new
JTextField
(
10
);
CustomerName
=
new
JTextField
(
10
);
ProductName
=
new
JTextField
(
10
);
ProductQuantity
=
new
JTextField
(
10
);
main
.
setLayout
(
new
GridLayout
(
0
,
1
));
JPanel
pane
=
new
JPanel
();
main
.
add
(
pane
);
pane
.
add
(
new
JLabel
(
"Customer Number: "
));
pane
.
add
(
CustomerNumber
);
pane
.
add
(
new
JLabel
(
"Customer Name: "
));
pane
.
add
(
CustomerName
);
pane
.
add
(
new
JLabel
(
"Product Name: "
));
pane
.
add
(
ProductName
);
pane
.
add
(
new
JLabel
(
"Product Quantity: "
));
pane
.
add
(
ProductQuantity
);
Insert
=
new
JButton
(
"Insert"
);
pane
.
add
(
Insert
);
Insert
.
addActionListener
(
new
Handler
());
GetProductName
=
new
JButton
(
"GetProductName"
);
pane
.
add
(
GetProductName
);
GetProductName
.
addActionListener
(
new
Handler
());
}
public
static
void
main
(
String
args
[]){
new
MainClass
();
}
}