java
java
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@drawable/images"
android:fontFamily="cursive"
android:paddingVertical="70dp"
android:text="Login"
android:textAlignment="center"
android:textColor="#090909"
android:textSize="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="35dp"
android:text="User name"
android:textColor="@color/colorAccent" />
<EditText
android:id="@+id/etuser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="@color/colorAccent" />
<EditText
android:id="@+id/etpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="35dp"
android:inputType="textPassword" />
<Button
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:background="@drawable/button_bg"
android:text="Login" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Account Yet? Create an Account"
android:textAlignment="center" />
</LinearLayout>
2nd
File handling
public class Testing {
public static void main(String[] args) {
//File Handling in Java
// //Java 11+
Files.writeString(filePath, text);
try {
Files.write(filePath, text.getBytes());
} catch(IOException e){
e.printStackTrace();
System.out.println("Unable to create file");
}
Exceptional handling
public class Testing {
public static void main(String[] args) {
String txt = "abc";
try{
char a = txt.charAt(5);
int x = Integer.parseInt(txt);
System.out.println("Hello");
System.out.println("Hi");
} catch(Exception e){
e.printStackTrace();
} finally {
try {
System.out.println("FInally Block");
} catch (Exception e) {
//TODO: handle exception
}
System.out.println("after try catch in finally");
}
System.out.println("Done");
}
}
Login frame
public class Loginframe extends JFrame {
public Loginframe(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400 , 400);
setResizable(false);
setLayout(null);
setTitle("Login");
btnlogin.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String UserName = txtuser.getText();
String Password =txtpass.getText();
if(UserName.equals("Waqas Rana" ) &&
Password.equals("218222")){
JOptionPane.showMessageDialog(Loginframe.this,
"Welcome");
}
else{
JOptionPane.showMessageDialog(Loginframe.this,
"Invalid");
}
});
chkshow.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(chkshow.isSelected()){
txtpass.setEchoChar((char)0);
} else
{
txtpass.setEchoChar('*');
}
}});
Db project
Add product frame
public class AddProductFrame extends JFrame {
public AddProductFrame(){
setLayout(null);
setSize(400, 400);
setResizable(false);
setLocationRelativeTo(null);
setAlwaysOnTop(true);
setTitle("Add Product");
btnSave.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(txtName.getText().isEmpty()){
} else if(txtPrice.getText().isEmpty()){
} else {
String name = txtName.getText();
float price = Float.parseFloat(txtPrice.getText());
try {
Connection con = DbHelper.connectDb();
String query = "INSERT INTO tbl_products(name,
price) VALUES(?, ?)";
PreparedStatement stmt =
con.prepareStatement(query);
stmt.setString(1, name);
stmt.setFloat(2, price);
int rowsAffected = stmt.executeUpdate();
stmt.close();
con.close();
if(rowsAffected > 0 ){
//product added succesffully
} catch (SQLException e) {
e.printStackTrace();
}
}
});
App
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
Database helper
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Login frame
public class LoginFrame extends JFrame {
public LoginFrame() {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(false);
setLayout(null);
setLocationRelativeTo(null);
setTitle("Login");
add(lblUsername);
add(txtUsername);
add(lblPassword);
add(txtPassword);
add(chkShowPassword);
add(btnLogin);
btnLogin.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String username = txtUsername.getText();
String password = txtPassword.getText();
try {
Connection con = DbHelper.connectDb();
ResultSet rs = stmt.executeQuery();
if(rs.isBeforeFirst()){
//it has rows
JOptionPane.showMessageDialog(getContentPane(),
"Login Successful");
stmt.close();
con.close();
setVisible(false);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(getContentPane(),
"Something went wrong with database!", "Error",
JOptionPane.ERROR_MESSAGE);
}
});
chkShowPassword.addActionListener(new
java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (chkShowPassword.isSelected()) {
txtPassword.setEchoChar((char) 0);
} else {
txtPassword.setEchoChar('*');
}
});
}
Mainframe
public class MainFrame extends JFrame {
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setLayout(null);
setTitle("Shop POS");
addProductsMenuItem.addActionListener(new
java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
updateProductsMenuItem.addActionListener(new
java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
UpdateProductFrame frame = new UpdateProductFrame();
frame.setVisible(true);
});
public UpdateProductFrame(){
setLayout(null);
setSize(400, 400);
setResizable(false);
setLocationRelativeTo(null);
setAlwaysOnTop(true);
setTitle("Update Product");
txtID.addKeyListener(new java.awt.event.KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
if(txtID.getText().isEmpty()){
return;
}
int id = Integer.parseInt(txtID.getText());
try {
Connection con = DbHelper.connectDb();
String query = "SELECT * FROM tbl_products WHERE
id=?";
PreparedStatement stmt =
con.prepareStatement(query);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if(rs.isBeforeFirst()){
rs.next();
String name = rs.getString("name");
float price = rs.getFloat("price");
txtName.setText(name);
txtPrice.setText(String.valueOf(price));
txtID.setEnabled(false);
txtName.setEnabled(true);
txtPrice.setEnabled(true);
} else {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
});
btnSave.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(txtName.getText().isEmpty()){
} else if(txtPrice.getText().isEmpty()){
} else {
String name = txtName.getText();
float price = Float.parseFloat(txtPrice.getText());
int id = Integer.parseInt(txtID.getText());
try {
Connection con = DbHelper.connectDb();
PreparedStatement stmt =
con.prepareStatement(query);
stmt.setString(1, name);
stmt.setFloat(2, price);
stmt.setInt(3, id);
txtID.setText("");
txtName.setText("");
txtPrice.setText("");
txtID.setEnabled(true);
txtName.setEnabled(false);
txtPrice.setEnabled(false);
} else {
});