Friday 26 April 2013

XML with CSS example(Styling your XML document)

Showing XML document with CSS

 Make Simple XML file and put css link refrence at top...(as shown in red) :
 
    <?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="collegexml.css"?>
<COLLEGE>
  <STUDENT>
    <NAME>ROHAN KANDWAL</NAME>
    <BRANCH>iNFORMATION TECHNOLOGY</BRANCH>
    <ROLLNO>800808122</ROLLNO>
    <YEAR>2008-12</YEAR>
    
  </STUDENT>

  <STUDENT>
    <NAME>SHRISTI BAGGA </NAME>
    <BRANCH>ELECTRONIC COMMUNICATION</BRANCH>
    <ROLLNO>800808122</ROLLNO>
    <YEAR>2009-13</YEAR>
    
  </STUDENT>

  <STUDENT>
    <NAME>SUNIL KUMAR</NAME>
    <BRANCH>COMPUTER SCIENCE</BRANCH>
    <ROLLNO>080800988</ROLLNO>
    <YEAR>2012-16</YEAR>
    
  </STUDENT>
  .
  .
  .
</COLLEGE>


Now your css file "collegexml.css":

COLLEGE
{
background-color: #66aacc;
width: 100%;
}
STUDENT
{
display: block;
margin-bottom: 50px;
margin-left: 13px;
}
NAME
{
color: #FF0060;
font-size:18pt;
}
BRANCH
{
color: #0070FF;
font-size: 16pt;
}
ROLLNO,YEAR
{
display: block;
color: #0865c0;
margin-left: 16pt;
}

output:

JDBC Sample Example

Simple JDBC (java Database connection) Example..

In this we are connecting to a database name "Students" and then retrieving value from a table "Registration" which exists with in the database :

import java.sql.*;

public class JDBCExample {
   // JDBC driver name and database URL
   String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   String DB_URL = "jdbc:mysql://localhost/STUDENTS";

   //  Database credentials
    String USER = "username";
   String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
     
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");
      
      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();

      String sql = "SELECT id, first, last, age FROM Registration";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
      rs.close();
      conn.close();
   }
catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
     }

   }//end main 
}//end JDBCExample