Regular expression to check url in JAVA

Written by admin on . Posted in Code Base


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

public static void main(String[] args) {

String urlPattern = "^http(s{0,1})://[a-zA-Z0-9_/\\-\\.]+\\.([A-Za-z/]{2,5})[a-zA-Z0-9_/\\&\\?\\=\\-\\.\\~\\%]*";

Scanner scanner = new Scanner(System.in);
System.out.println("Enter URL to check ");
String url = scanner.nextLine();

Pattern patt = Pattern.compile(urlPattern);

Matcher matcher = patt.matcher(url);
System.out.println(matcher.matches());

}
}

	

How to get all columns of table in Oracle

Written by admin on . Posted in Code Base

Here you will find the code base to search for the list of columns in a table in Oracle DB.
Note* : You need to provide the oracle DB related details in code(e.g. userId & password etc)

/*
 * Here we will learn to access columns in tables using DB Metadata
 */
package jbt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AccessTableCoulmn {

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {

		Class.forName("oracle.jdbc.OracleDriver");
		String serverName = "172.20.6.34";
		String portNumber = "1541";
		String sid = "NCSS10DV";
		String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
				+ sid;
		String username = "ncsdba";
		String password = "mig123";
		Connection conn = DriverManager.getConnection(url, username, password);

		Statement stmt = conn.createStatement();
		ResultSet rs = stmt
				.executeQuery("SELECT column_name, data_type, data_length"
						+ " FROM user_tab_columns"
						+ " WHERE table_name = 'JBT_TABLE'");

		while (rs.next()) {
			System.out.print(rs.getString(1));
			System.out.print(" ");
			System.out.print(rs.getString(2));
			System.out.print(" ");
			System.out.println(rs.getString(3));
		}

	}
}

 

Java code to connect to oracle database using jdbc

Written by admin on . Posted in Code Base

/*
 * Here we will learn to connect to DB
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;

public class CreateConnectionToDB {

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter DB host address");
		String serverName = scanner.next();

		System.out.println("Enter DB port");
		String portNumber = scanner.next();

		System.out.println("Enter DB userId");
		String username = scanner.next();

		System.out.println("Enter DB Password");
		String password = scanner.next();

		System.out.println("Enter DB SID or Service name");
		String sid = scanner.next();

		Class.forName("oracle.jdbc.OracleDriver");
		System.out.println("Oracle driver loaded Successfully.");

		String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
				+ sid;
		Connection conn = DriverManager.getConnection(url, username, password);

		System.out.println("Connection to DB is successful");

	}
}

 

How to create Table in DB using Java

Written by admin on . Posted in Code Base

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTableInDB {

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {

		Class.forName("oracle.jdbc.OracleDriver");

		String serverName = "";
		String portNumber = "1541";
		String sid = "";
		String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
				+ sid;
		String username = "";
		String password = "";
		Connection conn = DriverManager.getConnection(url, username, password);

		Statement stmt = conn.createStatement();
		stmt.executeUpdate("create table jbt_table(name varchar2(12))");
		System.out.println("Table Creation Successful");
	}

}

 

Find Default Date format of Oracle DB using Java

Written by admin on . Posted in Code Base

To check the default date format of Oracle Db find below the code base. You need to provide Oracle related details in programme.

package com.db;

/*
 * Here we will learn to get default Date format of Oracle DB.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DefaultDateFormat {

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {

		Class.forName("oracle.jdbc.OracleDriver");

		String serverName = "";
		String portNumber = "1541";
		String sid = "";
		String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
				+ sid;
		String username = "";
		String password = "";
		Connection conn = DriverManager.getConnection(url, username, password);

		Statement stmt = conn.createStatement();
		ResultSet rs = stmt
				.executeQuery("SELECT VALUE  FROM v$parameter WHERE UPPER (NAME) = 'NLS_DATE_FORMAT'");

		while (rs.next()) {
			System.out.print(rs.getString(1));
		}
	}
}

 

Java check if file readable

Written by admin on . Posted in Code Base

import java.io.File;
import java.util.Scanner;

/*
 * Here we will learn to check if file is read only or not?
 */
public class IsFileReadOnly {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the path of File");
		String filePath = scanner.nextLine();
		File file = new File(filePath);

		if (file.isDirectory()) {
			System.err.println("Given path is of Directory and not of File.");
			System.exit(0);
		} else {
			if (file.canWrite()) {
				System.out.println("File is writtable.");
			} else
				System.err
						.println("File is not Read Only.");
		}

	}
}

 

How to check if directory is hidden in Java

Written by admin on . Posted in Code Base

import java.io.File;
import java.util.Scanner;

/*
 * Here we will learn to check if given directory is hidden or not.
 */
public class IsDirHidden {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the path of Directory to check.");
		String dirPath = scanner.nextLine();

		File file = new File(dirPath);
		if (file.isDirectory()) {
			if (file.isHidden())
				System.out.println("Directory is Hidden");
			else
				System.out.println("Directory is not Hidden");
		} else
			System.err.println("This is not a directory");
	}
}

 

Java rename directory example

Written by admin on . Posted in Code Base

This example will show you the way to rename any folder in Java.

import java.io.File;
import java.util.Scanner;

/*
 * Here we will learn to rename a Directory.
 */
public class RenameDir {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the path of directory to rename.");
		String dirPath = scanner.nextLine();
		File dir = new File(dirPath);
		if (!dir.isDirectory()) {
			System.err.println("There is no directory @ given path");
			System.exit(0);
		}

		System.out
				.println("Enter new name of directory(Only Name and Not Path).");
		String newDirName = scanner.nextLine();

		File newDir = new File(dir.getParent() + "\" + newDirName);
		dir.renameTo(newDir);

		System.out.println("Done");
	}
}

 

How to read a File using Scanner Class

Written by admin on . Posted in Code Base

Here we are discussing about reading the file using Scanner.

To learn to read File using BufferedInputStream  Click Here.

To learn to read File using BufferedReader  Click Here.

package JavaIOExample;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*
 * Here we will learn to read the file using Scanner.
 */
public class ReadTheFileUsingScanner {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the File path");
		String filePath = scanner.next();

		/*
		 * Creates a new File instance by converting the given pathname string
		 * into an abstract pathname.
		 */
		File file = new File(filePath);

		Scanner scannerFile = null;

		try {
			/*
			 * Constructs a new Scanner that produces values scanned from the
			 * specified file. Bytes from the file are converted into characters
			 * using the underlying platform's default charset.
			 */
			scannerFile = new Scanner(file);

			/*
			 * Returns true if this scanner has another token in its input.
			 */
			while (scannerFile.hasNext())
				/*
				 * Finds and returns the next complete token from this scanner.
				 * A complete token is preceded and followed by input that
				 * matches the delimiter pattern.
				 */
				System.out.println(scannerFile.next());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			scanner.close();
		}
	}

}

 

How to read a File using BufferedInputStream

Written by admin on . Posted in Code Base

Here we are discussing about reading the file using BufferedInputStream.

To learn to read File using BufferedReader  Click Here.

To learn to read File using Scanner  Click Here.

package JavaIOExample;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

/*
 * Here we will learn to read the file using BufferedInputStream. 
 */
public class ReadTheFileUsingBufferedInputStream {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the File path");
		String filePath = scanner.next();

		BufferedInputStream bis = null;
		DataInputStream dis = null;
		FileInputStream fis = null;

		/*
		 * Creates a new File instance by converting the given pathname string
		 * into an abstract pathname. 
		 */
		File file = new File(filePath);

		try {
			fis = new FileInputStream(file);

			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);

			while (dis.available() != 0) {
				/*
				 * Read the next line of text from this input stream.
				 * Deprecated
				 */
				System.out.println(dis.readLine());
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				bis.close();
				dis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}