Java Database Connectivity and JavaScript Objects
JDBC
JDBC stands for Java Database Connectivity, an API in Java that allows Java programs to connect and interact with a database. In other words, JDBC is the bridge between Java code and a database system. It is installed automatically with JDBC software. It provides classes and interfaces to connect or communicate Java applications with a database system.
Java Database Connectivity with 5 Steps
1) Register the driver class
The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. |
- Class.forName(‘oracle.jdbc.driver.OracleDriver’);
2) Create the connection object
The getConnection() method of DriverManager class is used to establish a connection with the database. |
- Connection con=DriverManager.getConnection(‘jdbc:oracle:thin:@localhost:1521:xe’,’system’,’password’);
3) Create the Statement object
The createStatement() method of Connection interface is used to create a statement. The object of the statement is responsible for executing queries with the database. |
- Statement stmt=con.createStatement();
4) Execute the query
The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. |
- ResultSet rs=stmt.executeQuery(‘select * from emp’);
- while(rs.next()){ System.out.println(rs.getInt(1)+’ ‘+rs.getString(2)); }
5) Close the connection object
By closing the connection object, the statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection. |
con.close();
Document Object Model
The document object represents the whole HTML document. When an HTML document is loaded in the browser, it becomes a document object. It is the root element that represents the HTML document. It has properties and methods. By the help of the document object, we can add dynamic content to our web page.
Accessing field value by document object
In this example, we are going to get the value of the input text by the user. Here, we are using document.form1.name.value to get the value of the name field.
form1 is the name of the form.
name is the attribute name of the input text.
value is the property that returns the value of the input text.
- function printvalue(){ var name=document.form1.name.value; alert(‘Welcome: ‘+name); };
- name=’form1′>
- Enter Name: type=’text’ name=’name’>
- type=’button’ onclick=’printvalue()’ value=’print name’>
Javascript Data Types
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript. 1.->Primitive data type 2->Non-primitive (reference) data type
JavaScript is a dynamic type language, which means you don’t need to specify the type of the variable because it is dynamically used by the JavaScript engine. You need to use var here to specify the data type. It can hold any type of values such as numbers, strings, etc. For example: var a=40;//holding number , var b=’Rahul’;//holding string
JavaScript primitive data types
There are five types of primitive data types in JavaScript. They are as follows:
Data Type | Description |
String | represents a sequence of characters e.g. ‘hello’ |
Number | represents numeric values e.g. 100 |
Boolean | represents a boolean value either false or true |
Undefined | represents an undefined value |
Null | represents null i.e. no value at all |
JavaScript non-primitive data types
The non-primitive data types are as follows:
Data Type | Description |
Object | represents an instance through which we can access members |
Array | represents a group of similar values |
RegExp | represents a regular expression |
JavaScript Objects
A JavaScript object is an entity having state and behavior (properties and methods). For example: car, pen, bike, chair, glass, keyboard, monitor, etc. JavaScript is an object-based language. Everything is an object in JavaScript. JavaScript is template-based, not class-based. Here, we don’t create a class to get the object. Instead, we directly create objects.
Creating Objects in JavaScript
1) JavaScript Object by object literal
- 1 emp={id:102,name:’Shyam Kumar’,salary:40000} 2 document.write(emp.id+’ ‘+emp.name+’ ‘+emp.salary);
2) By creating an instance of Object
- var emp={}; 3 emp.id=101; emp.name=’Ravi Malik’; 4 emp.salary=50000; document.write(emp.id+’ ‘+emp.name+’ ‘+emp.salary);
3) By using an Object constructor
Here, you need to create a function with arguments. Each argument value can be assigned in the current object by using the this keyword.
The this keyword refers to the current object.
The example of creating an object by an object constructor is given below.
- function emp(id,name,salary){ this.name=name; } document.write(e.id+’ ‘+e.name+’ ‘+e.salary);
What is JavaScript
JavaScript (JS) is a lightweight object-oriented programming language used by several websites for scripting webpages. It is an interpreted, full-fledged programming language that enables dynamic interactivity on websites when applied to an HTML document. It was introduced in 1995 for adding programs to webpages in the Netscape Navigator browser. Since then, it has been adopted by all other graphical web browsers. With JavaScript, users can build modern web applications to interact directly without reloading the page every time. Traditional websites use JS to provide several forms of interactivity and simplicity.
