File Handling in Java Using Filewriter and Filereader
File Handling in Java Using Filewriter and Filereader
Java FileWriter and FileReader classes are used to write and read data from text files (they
are Character Stream classes). It is recommended not to use the FileInputStream and
FileOutputStream classes if you have to read and write any textual information as these are
Byte stream classes.
FileWriter
FileWriter is useful to create a file writing characters into it.
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.util.Scanner;
public class ReadFile
{
public static void main(String[] args) throws FileNotFoundException
{
FileInputStream fis=new FileInputStream("myFile.txt");
Scanner in=new Scanner(fis);
while(in.hasNext()){
System.out.println(in.nextLine());
}
}
}
Class Description
To display the current date, import the java.time.LocalDate class, and use its now() method:
import java.time.*;
LocalDate date=LocalDate.now();
System.out.println(date);
LocalTime time=LocalTime.now();
System.out.println(time);
OUTPUT :
2020-03-12
10:47:48.824
Regular Expressions is an API for defining String patterns that can be used for searching,
manipulating a string in Java.
Email validation and passwords are few areas of strings where Regex are widely used to define
the constraints.
Regular Expressions are provided under java.util.regex package.
This consists of 3 classes and 1 interface.
The java.util.regex package provides following classes and interfaces for regular expressions.
1. MatchResult interface
2. Matcher class
3. Pattern class
4. PatternSyntaxException class
import java.util.regex.*;
class RegExDemo {
int count=0;
Pattern p=Pattern.compile("ab");
Matcher m=p.matcher("ababbaba");
while(m.find())
count++;
System.out.println(m.start());
- JDBC is a Java API to connect and execute the query with the database. It is a part of
JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database.
There are four types of JDBC drivers:
- The java.sql package contains classes and interfaces for JDBC API.
- Following is the architectural diagram, which shows the location of the driver manager
with respect to the JDBC drivers and the Java application −
Before JDBC, ODBC API was the database API to connect and execute the query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC
drivers (written in Java language).
1. Loading Driver
2. Establishing connection
3. Preparing statement
4. Executing Statement
5. Getting Result
6. Closing Database connection
Loading Driver:
Class.forName(“Oracle.jdbc.driver.OracleDriver”);
Establishing Connection:
- When getConnection is called the DriverManager will attempt to locate a suitable driver
from amongst those loaded at initialization and those loaded explicitly.
url=”jdbc:oracle:thin:@localhost:1521:XE”
import java.sql.*;
import java.util.*;
import java.io.*;
public class jdbcconnection {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system",
"manager");
Statement st=con.createStatement();
String sql= "select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("id")+" "+rs.getString("name"));
}
con.close();
}
Output:
10 sandip
20 vikas
1. Loading Driver
2. Establishing connection
3. Preparing statement
4. Executing Statement
5. Getting Result
6. Closing Database connection
READ procedures : Reads the table records based on the primary keynoted within the input
parameter.
UPDATE procedures : Executes an UPDATE statement on the table based on the specified
primary key for a record within the WHERE clause of the statement.
1. Simple statement
2.Prepared Statement
3.Collable Statement
Simple Statement
st.executeUpdate(“insert”);
st.executeUpdate(“delete”);
st.executeQuery(“select”);
i.e. if we want to execute multiple queries then we should go for simple Statement object. In
which each query will compile every time.
Per Query= Request time + Compile Time + Execution time + Response Time
- if we want to work with only one query, but should be executed multiple times then we
should go for Prepared Statement.
- Used for execute single query for multiple time
- If we want to execute another query then we need to create new prepared object again.
- In prepared statement query will compile only once.
Pst.executeUpdate();
Pst.executeUpdate();
Pst.executeUpdate();
import java.sql.*;
import java.util.*;
import java.io.*;
public class jdbcconnection {
Class.forName("oracle.jdbc.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system",
"manager");
Statement st=con.createStatement();
//String sql= "select * from student";
ResultSet rs=st.executeQuery("select * from stud");
while(rs.next()) {
System.out.println(rs.getInt("id")+"... "+rs.getString("name"));
}
con.close();
}
static
{
//STEP 1 : Registering The Driver Class
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable To Load The Driver class");
}
}
try
{
//Database Credentials
stmt = con.createStatement();
int i = stmt.executeUpdate(sql);
if(i == 0)
{
System.out.println("Table is created");
}
else
{
System.out.println("Table is not created");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//STEP 5 : Closing The DB Resources
try
{
if(con!=null)
{
con.close();
con=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
static
{
//STEP 1 : Registering The Driver Class
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable To Load The Driver class");
}
}
try
{
//Database Credentials
stmt = con.createStatement();
String sql = "INSERT INTO collg VALUES" +"(500, 'rajesh', 'Sharma', 'clerk')";
int i = stmt.executeUpdate(sql);
if(i != 0)
{
System.out.println("Row is created");
}
else
{
System.out.println("Row is not created");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//STEP 5 : Closing The DB Resources
import java.sql.*;
public class SelectStatementExample {
static
{
//STEP 1 : Registering The Driver Class
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable To Load The Driver class");
}
}
ResultSet rs = null;
try
{
//Database Credentials
rs = stmt.executeQuery(sql);
while (rs.next())
{
System.out.println("ID :"+rs.getInt(1));
System.out.println("Designation :"+rs.getString(4));
System.out.println("-------------------");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//STEP 5 : Closing The DB Resources
try
{
if(rs!=null)
{
rs.close();
rs=null;
try
{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if(con!=null)
{
con.close();
con=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
import java.sql.*;
public class UpdateStatementExample {
static
{
//STEP 1 : Registering The Driver Class
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable To Load The Driver class");
}
}
try
{
//Database Credentials
int i = stmt.executeUpdate(sql);
if(i != 0)
{
System.out.println("Record is updated");
}
else
{
System.out.println("Record is not updated");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//STEP 5 : Closing The DB Resources
try
{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if(con!=null)
{
con.close();
con=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
import java.sql.*;
public class DeleteStatementExample {
static
{
//STEP 1 : Registering The Driver Class
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable To Load The Driver class");
}
}
try
{
//Database Credentials
stmt = con.createStatement();
int i = stmt.executeUpdate(sql);
if(i != 0)
{
System.out.println("Record is deleted");
}
else
{
System.out.println("Record is not deleted");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
//STEP 5 : Closing The DB Resources
try
{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if(con!=null)
{
con.close();
con=null;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}