Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JDBC Why ODBC cannot be used directly with Java Program? ODBC can not used directly with java program due to various reason describe below. 1. ODBC can not be used directly with java because it uses C interface. This will have drawbacks in the security, implementation and robustness. 2. ODBC makes use of pointers which have been removed from java. 3. ODBC mixes simple and advanced feature together and has complex structure. Hence, JDBC came into existence JDBC API interface is made up of 4 main interfaces. 1. java .sql .DriverManager 2. java.sql.Connection 3. java.sql.Statement 4. java.sql.Resultset The Main Objects of the JDBC API include: 1. A Data Source object is used to establish connection. Although the driver manager can also be used to establish a connection. 2. A Connection object controls the connection to the database. An application uses the connection object to create statement. 3. Statement object are used for executing SQL queries. 4. A Result set object act like a workspace to store result of queries. A result set is returned to an application when a SQL query is executed by a statement object. steps for connecting and querying database JDBC is an API(Application Programming Interface)which consists of a set of java classes, interface and exceptions with the help of JDBC programming interface, java programmers can request a connection with a database, then send query statement using SQL and receive result of processing. STEPS TO CONNECT TO A DATABASE: The Interface and classes of the JDBC API are present inside the package called as java.sql package.There any application using JDBC API must import java.sql package in its code. Import java.sql.*; STEP 1: Load the Drivers The first step in accessing the database is to load an appropriate driver.You can use one driver from the available four drivers which are described earlier.However, JDBC -ODBC Driver is the most preferred driver among developers.If you are using any other type of driver,then it shoud be installed on the system .in order to load the driver, you have to give the following syntax: Class.ForName(“sun.jdbc.odbc.JdbcOdbcDriver”); Step 2: Make the Connection The getConnection()method of the Driver Manager class is called to obtain the connection object. The syntax is Connection conn=DriverManager.getConnection(“jdbc:odbc:<DSN NAME>”); Here note that getConnection() is a static method, meaning itshould be accessed along with class associated with the method.The DSN(DATA source Name) neme is the name which you gave in the control panel->ODBC while registering the Database or table. Step 3: Create JDBC Statement A statement object is used to send SQL query to the database management system .you can simply create a statement object and then execute it.Connection object conn here to create the statement object “stmt” the syntax is. Statement stmt=conn.createStatement(); As mentioned earlier we may use prepared Statement or callable Statement according to the requirement. Step 4: Execute the Statement In order to execute the query you have to obtain the Result Set object similar to Record Set in Visual Basic and called the execute Query()method of the statement. Actually the recordSet object contains both the data returned by query and the method for data retrival. The syntax is Result Set rs=stmt.executeQuery(“select * from student”); The executeUpdate() method is called whenever there is a delete or an update operation. Step 5: Navigation or looping through the ResultSet The ResultSet object contains rows of data that is parsed using the next() method like rs.next().we use the getXXX() like getInt() to retrive Integer fields and getString() for string fields. System.out.println(rs.getInt(“id”)); Step 6: Close the Connection and Statement Objects After performing all the above steps you must close the connection, statement and Result set object by calling the close () method. ResultSet object with rs.close(); Statement object with Stmt.close(); Connection object with Con.close //Reading Data from Database import java.sql.*; class Example1 { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException cnf) { System.out.println("Error : "+cnf); } try { Connection con=DriverManager.getConnection("jdbc:odbc:emp"); Statement smt=con.createStatement(); ResultSet rs=smt.executeQuery("select * from employee"); while(rs.next()) { System.out.println("empid="+rs.getInt(1)+"ename="+rs.getString(2)); } } catch(SQLException se) { System.out.println("Error in SQL : "+se); }} } //Writing Data into Database import java.sql.*; class Example2 { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException cnf) { System.out.println("Error : "+cnf); } try { Connection con=DriverManager.getConnection("jdbc:odbc:emp"); Statement smt=con.createStatement(); int i=smt.executeUpdate("INSERT INTO employee " + "VALUES (1009,'kamal')"); System.out.println(i); if(i==1) { System.out.println("inserted"); } con.close(); } catch(SQLException se) { System.out.println("Error in SQL : "+se); } } }