diff --git a/MySQLConnect.java b/MySQLConnect.java new file mode 100644 index 0000000..56c9568 --- /dev/null +++ b/MySQLConnect.java @@ -0,0 +1,29 @@ +package proj_mysqlconnect; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import javax.swing.JOptionPane; + +/** + * + * @author ErSKS + */ +public class MySQLConnect { + + public static Connection conn; + + public static Connection connectDb() { + try { + Class.forName("com.mysql.jdbc.Driver"); + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_db", "java", ""); + System.out.println("Database Connected Successfully !"); + return conn; + } catch (ClassNotFoundException | SQLException e) { + System.out.println(e.getMessage()); + JOptionPane.showMessageDialog(null, "Database cannot be connected !"); + System.exit(0); + } + return null; + } +} diff --git a/PrimeNumber.java b/PrimeNumber.java new file mode 100644 index 0000000..53a2010 --- /dev/null +++ b/PrimeNumber.java @@ -0,0 +1,48 @@ +package cjt; + +/** + * + * @author ErSKS + */ +public class PrimeNumber { + + public static void main(String[] args) { + System.out.println("Q1. Checking Prime Numbers:"); + System.out.println("Prime Check: 0 false:" + isPrime(0)); + System.out.println("Prime Check: 1 false:" + isPrime(1)); + System.out.println("Prime Check: 2 true:" + isPrime(2)); + System.out.println("Prime Check: 3 true:" + isPrime(3)); + System.out.println("Prime Check: 4 false:" + isPrime(4)); + System.out.println("Prime Check: -4 false:" + isPrime(-4)); + + System.out.println("\nGenerating Prime Numbers less than 100:"); + generatePrimes(100); + System.out.println("\n"); + } + + static boolean isPrime(int n) { + if (n < 2) { + return false; + } + int countFactor = 0; + boolean result = true; + for (int i = 1; i < n && result == true; i++) { + if (n % i == 0) { + countFactor++; + } + if (countFactor > 1) { + result = false; + } + } + return result; + } + + static void generatePrimes(int n) { + + for (int i = 1; i < n; i++) { + if (isPrime(i) == true) { + System.out.print(i + " "); + } + } + } +}