Mathematics(IX, X, XI, XII) CBSE & UP Board & CSE(C programming, Python, JAVA, Data Structure, IoT, IT, CBNST etc.)
Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts
Saturday, December 22, 2018
Monday, November 26, 2018
How to connect JAVA application with mysql database
Example:
package javaapplication36;
import java.sql.*;
public class JavaApplication36 {
public static void main(String[] args) {
try{
//Class.forName("com.mysql.jdbc.Driver");
// comment above line if jar file is uploaded manually in your project
// comment above line if jar file is uploaded manually in your project
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","mysql@123");
//here test is database name, 3306 is default port, root is username and mysql@123 is password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from tab1");
// tab1 is a table in test database
// tab1 is a table in test database
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
con.close();
con.close();
}
catch(Exception e)
{ System.out.println(e);
}
}
}
To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.
Step 2: download JDBC Driver for MySQL (Connector/J)
Step 3: select operating system: Platform Independent and click on the download
Step 4: click on No thanks, just start my download at the bottom
Step 5: Extract downloaded file
Step 6: open your NetBeans project //Manually upload jar file in the project
Step 7: Right-click on libraries folder of your project
Step 8: click on add Jar/Folder…
Step 9: select jar file mysql-connector-java-8.0.13 from downloaded folder
Step 10: now compile your program
Wednesday, November 21, 2018
JAVA program to count the occurrence of each letter using hashing concept
Ex:
Write a program that creates a char type array of 100 lowercase letters randomly and counts the occurrence of each letter in the array.
JAVA Code:
Write a program that creates a char type array of 100 lowercase letters randomly and counts the occurrence of each letter in the array.
package javaapplication3;
import java.util.Arrays;
import java.util.Random;
public class JavaApplication3
{
public static void main(String[] args)
{
// TODO code application logic here
JavaApplication3 obj=new JavaApplication3();
Random o=new Random();
int max=122,min=97;
char arr[]=new char[100];
for(int i=0;i<100;i++)
{
int n= o.nextInt((max-min)+1);
n=n+min;
arr[i]=(char)n;
}
System.out.println("Hundred Random letters:");
for(int i=0;i<100;i++)
{ if(i==20 || i==40 || i==60 || i==80)
{System.out.print("\n");}
System.out.print(" "+arr[i]);
}
int occ[]=new int[26];
Arrays.fill(occ, 0);
System.out.print("\n");
System.out.println("Occurrence of each letter:\n");
obj.fintoccurrence(arr,occ);
}
void fintoccurrence(char arr[],int occ[])
{
for(int i=0;i<100;i++)
{
int oc= arr[i]-97;
occ[oc]++;
}
int as=97;
for(int i=0;i<26;i++)
{ if(i==10 || i==20)
{ System.out.print("\n"); }
System.out.print("\t"+occ[i]+" "+(char)as);
as++;
}
}
}
Sunday, November 11, 2018
Define the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class by extending the Exception class, and modify the constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows: /** Construct a triangle with the specified sides */ public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { // Implement it }
JAVA Code: Topic: Exception Handling
package javaapplication34;
import java.util.Scanner;
class Triangle {
private double side1=1.0;
private double side2=1.0;
private double side3=1.0;
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException
{ if(side1+side3 <= side2 || (side1 + side2) <= side3 || (side2 + side3) <= side1)
{ throw new IllegalTriangleException();
}
else {
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
}
}
class IllegalTriangleException extends Exception
{
IllegalTriangleException()
{ super("Exception: Invalid Triangle Side Numbers");
}
}
public class javaapplication34 { public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter Three Double Side: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
try{
Triangle tr=new Triangle( side1, side2, side3);
System.out.println("Valid triangle sides");
}
catch(IllegalTriangleException e)
{
System.out.println(e);
}
}
}
OUTPUT:
Subscribe to:
Posts (Atom)

