توضیح در مورد مراحل برقراری ارتباط با دیتابیس در جاوا

  1. 1- Step1: before making a connection we need to register the driver. we can achieve it using 2 different methods: 1-register driver methods  2-forname method.
    • registerDriver method belongs to DriverManager class. and it is a static method which gets the argument as object of driver class. example:DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver())
    • forename is the method belongs to the class "Class" on the java.lan package. It is a static method, and takes an argument as a string type of the class name. Example: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    2-Step2: Making a connection: after registering the driver we need to make connection with the Oracle database. for this we use getConnection() method from  connection interface. We need to create the object of Connection interface for making a connection but a Connection is an interface, we can not create object using "new" keyboard. There is a method in Java ApI  named "getConnection()" inside DriverManagerClass. basically this method has 3 arguments. 1-URL of database 2-username of database 3-pass of database
    getConnection() is a static method and return the object of connection interface
    example; connection con = DriverManager.getConnection(url,unam,pass)
    3-Step3:to create statement object although connection has been created but we can not pass any SQL statement directly to the database. to pass the SQL statement we need to create object of statement interface. the method createStatement is used to achieve the same which is belongs to connection interface and it is a non static method. this return the object of Statement interface.
    Example: Statement stmt = con.CreateStatement("Select * from emp")
    4-step4: All the Query in SQL had divided to 2 types: 1-Select Statement type  2-non select statement type. 
  • Select statement type: this statement are used to retrieve the content of an object. object is nothing but a table. it returns a table type of data. which we have to catch after executing select statement.
  • non select statement type: after running this Query, it returns an integer value.
  • 5-Step5: Execute Query. ResultSet is an interface that is capable to catch the table type object. Example: ResultSet rs = stmt.executeQuery("Select * from emp") using method executedUpdate method we are passing the non select type of statement to the database. this return an integer value after running the SQL statement.
  • Example: int x = stmt.executeUpdate("insert into emp values('name','sal')");
    6-step6: present the data. after running the Query the table return by the database.
    it is stored at resultSet interface. we have to retrieve from result set and display
    these data:
    • next method; this method returns a boolean value. if next row is present then control goes to the next row.
    • methods to retrieve attributes:retrieving integer data if the column is the integer type:method is GetInt(); getInt() method takes 2 type of argument 1-poition of the column 2-name of the column. Example: no = rs.getInt("EMPNO"). to retrive the string data if the column is String type the method is GetString(). Example: name = rs.getString("EName"); Eample: rs.getFloat("SAL");
نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد