Sunday 22 June 2014

Java Database Connection

        public static String driver="com.mysql.jdbc.Driver";
        public static String url="jdbc:mysql://localhost:3306/dbname;
public static String user="root";
public static String password="root";
    

public static Connection getConnection(){
java.sql.Connection con =null;

try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
if(con!=null){
                                 System.out.println("connection established");
}
else{
System.out.println("connection not established");
}

}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}

        public static void closeConnection(Connection conn){
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

Saturday 21 June 2014

How to write file in JAVA


  • Create S3.txt filein E: drive
  • After creating file run this code


                        File file = new File("E:\\Check\\S3.txt");
System.out.println("Enter  Content");
FileWriter out = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(out);
BufferedReader br  =new BufferedReader(new InputStreamReader(System.in));
bw.write(br.readLine());
bw.close();

Find Character in file and remove from file the file in JAVA

  • First create two files with name S1.txt and S2.txt in E: drive
  • This program replace all occurrence of 'a' or 'A' character from file S1 and write into S2 file


               File file1 = new File("E:\\S2.txt");
File file2 = new File("E:\\S2.txt");
FileInputStream fis1 = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
StringBuffer sub1 = new StringBuffer();
int ch;
if(file1.exists())
{
while((ch=fis1.read()) != -1)
{

if((char)ch != 'a' && (char)ch != 'A')
{
 fos.write((char)(ch));
}
 
 
}
fis1.close();
 
}
int ch2;
FileInputStream fis2 = new FileInputStream(file2);
if(file2.exists())
{
while((ch2=fis2.read()) != -1)
{
sub1.append((char)ch2);
}
}

System.out.println("File Created Without A Character");
System.out.println(sub1);
 
 
}

Check Two files are equal or not in java

                 // TODO Auto-generated method stub

File file1 = new File("E:\\Check\\m1.txt");
File file2 = new File("E:\\Check\\m2.txt");

FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);

StringBuffer sub1 = new StringBuffer();
StringBuffer sub2 = new StringBuffer();


int i,j;
int ch1,ch2;
if(file1.exists() && file2.exists()){
while((ch1=fis1.read()) != -1){
sub1.append((char)ch1);

}
fis1.close();
while((ch2=fis2.read()) != -1){
sub2.append((char)ch2);

}
fis2.close();
}
boolean flag = true;
String temp = null;
for(i=0,j=0; i<sub1.length();i++,j++){
if(sub1.charAt(i) != sub2.charAt(j)){
temp = (char)sub1.charAt(i)+"";
flag = false;
}

}
if(flag == false){
System.out.println("Both Files are not Same--"+temp);
}
else{
System.out.println("Both Are Same");
file2.delete();
System.out.println("Second File is SuccessFully Deleted");
}