Lesson 9
Date
public void Dat(){
Calendar ca = new GregorianCalendar();
int y = ca.get(Calendar.YEAR);
int m = ca.get(Calendar.MONTH);
int d = ca.get(Calendar.DATE);
D.setText(y + "/" + (m+1) + "/" + d);
}
Combo Box
public void CB(){
String sql = "select * from Product";
try{
st = c.prepareStatement(sql);
rs = st.executeQuery();
while(rs.next()){
c1.addItem(rs.getString("Name"));
}
}
catch(Exception e){}
}
Win Close
public void WinClose() {
try{
rs.close();
st.close();
WindowEvent W = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(W);
}catch(Exception e){}
}
Update Table
public void Updatetb55() {
String sql = "select * from Sell";
try {
st = c.prepareStatement(sql);
rs = st.executeQuery();
b3.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
}
}
Combo Box Click
String R = c1.getSelectedItem().toString();
t1.setText(R);
try{
String sql = "select * from Product where Name like ? ";
st = c.prepareStatement(sql);
st.setString(1, t1.getText());
rs = st.executeQuery();
if(rs.next()){
t2.setText(rs.getString("Price"));
}
}
catch(Exception e){}
Text Area
public void TT(){
T.append("\n\t\t\t ໃບສັ່ງຈ່າຍ\n\nຮ້ານ ໂມເດີ້ນຄອມພິວເຕີ້\t\t\t\tວັນທີ "+D.getText()+"\nໂທ: 22229999\t\t\t\tເລກທີ:" +N.getText()+
"\n=================================================================================\n"
+ "ລາຍການ\t\tລາຄາ\t\tຈໍານວນ\t\tລວມເງິນ \n"
+ "=================================================================================\n");
}
7. Add TextArea
try{
String sql ="insert into Sell (Item, Price, Amount, Total) values (?, ?, ?, ?)";
st = c.prepareStatement(sql);
st.setString(1, t1.getText());
st.setString(2, t2.getText());
st.setString(3, t3.getText());
st.setString(4, t4.getText());
st.execute();
}
catch(Exception e){JOptionPane.showMessageDialog(null, "Not Save");}
finally {
try {
rs.close();
st.close();
} catch (Exception e) {
}
}
if(1==1){
T.append(
"\n"+t1.getText()+"\t\t"+t2.getText()+"\t\t"+t3.getText()+"\t\t"+t4.getText()+"\n"
);
}
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
String sql = "select * from Sell";
try {
st = c.prepareStatement(sql);
rs = st.executeQuery();
b3.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
}
8. Table Click
int row = b3.getSelectedRow();
t1.setText(b3.getModel().getValueAt(row, 0).toString());
String sql = "select * from Sell where Item = '"+ t1.getText()+"' ";
try {
st = c.prepareStatement(sql);
rs = st.executeQuery();
if (rs.next()) {
t2.setText(rs.getString("Price"));
t3.setText(rs.getString("Amount"));
t4.setText(rs.getString("Total"));
}
} catch (Exception e) {
}
try {
String sq = "delete from Sell where Item = '"+ t1.getText()+"' ";
st = c.prepareStatement(sq);
st.execute();
Updatetb55();
} catch (Exception e) {
}
9. Sum
float sum = 0;
//int sum1 = 0;
for(int i=0; i<b3.getRowCount();i++){
sum = sum + Float.parseFloat(b3.getValueAt(i, 3).toString());
//sum1 = sum1 + Integer.parseInt(b3.getValueAt(i, 2).toString());
}
r3.setText(String.format("%.2f", sum));
float R = Float.parseFloat(r1.getText());
R = sum/R;
r4.setText(String.format("%.2f", R));
float R1 = Float.parseFloat(r2.getText());
R1 = sum/R1;
r5.setText(String.format("%.2f", R1));
T.append("\n\n\t\t\t\t\tລວມ\tກີບ "+r3.getText()+"\n\t\t\t\t\t\tບາດ "+r4.getText()+"\n\t\t\t\t\t\tໂດລາ "+r5.getText());
10. Print
int J = JOptionPane.showConfirmDialog(null, "Confirm print", "Print", JOptionPane.YES_NO_OPTION);
if(J==0){
try{
T.print();
String S = "insert into SOwner (Date, Money) values (?,?)";
st = c.prepareStatement(S);
st.setString(1, D.getText());
st.setString(2, r3.getText());
st.execute();
String sql = "delete from Sell";
st = c.prepareStatement(sql);
st.execute();
// JOptionPane.showMessageDialog(null, "Delete");
st.close();
rs.close();
}
catch(Exception e){}
finally {
try {
rs.close();
st.close();
} catch (Exception e) {
}
}
WinClose();
}
11. Rate show
public void R(){
String sql = "select * from Rate";
try {
st = c.prepareStatement(sql);
//st.setString(1, "11");
rs = st.executeQuery();
if(rs.next()){
r1.setText(rs.getString("Bath"));
r2.setText(rs.getString("Dolar"));
}
} catch (Exception e) {
}
finally {
try {
rs.close();
st.close();
} catch (Exception e) {
}
}
}
12. Date time
import java.time.LocalTime; // import the LocalTime class
public class Main {
public static void main(String[] args) {
LocalTime myObj = LocalTime.now();
System.out.println(myObj);
LocalDate myObj = LocalDate.now(); // Create a date object
System.out.println(myObj); // Display the current date
}
}
DT2
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
public class Main {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
}
}
DT3
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
public class Main {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before Formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After Formatting: " + formattedDate);
}
}
13. Full window
setExtendedState(JFrame.MAXIMIZED_BOTH);
14. Method
public void Academ(){
c = DBCon.A();
try{
String sql = "select distinct Academic from StudentRe ";
st = c.prepareStatement(sql);
rs = st.executeQuery();
if(rs.next()){
t6.setText(rs.getString("Academic"));
}
}
catch(Exception e){JOptionPane.showMessageDialog(null, "");}
finally {try {rs.close(); st.close(); c.close(); } catch (Exception e) {} }
}
15. Key Enter
private void t12KeyReleased(java.awt.event.KeyEvent evt) { }
16. Loop sum
float sum = 0;
//int sum1 = 0;
for(int i=0; i<b3.getRowCount();i++){
sum = sum + Float.parseFloat(b3.getValueAt(i, 1).toString());
//sum1 = sum1 + Integer.parseInt(b3.getValueAt(i, 2).toString());
}
17. close window
this.setVisible(false);