Multitasking using threads: once a program goes for execution it is the job of processor to execute the program. if the program contains some statements that is eligible to transfer the data at that time process goes for "idle" state. to make our process com out from Idle state we are using multi threading concept.
Multitasking has two types: 1-thread base 2-process based multitasking.
what is process? the program under execution is known as process.
what is thread? small part of program is known as thread.
Java Doesn't support process based multitask. we can not execute two programs at the same time.
Java supports only thread based multitasking.
processor decides how many second it executes.
to achieve thread based multitasking in Java we have to follow these steps:
We have to write all those statements inside 1 method which will execute as a thread.
There is a method in java API named "public void run" which can be used to identify the method as a thread and execute it as a thread.
in java API there is the method "start" if we dont use start method to call run, then run method will be executed as a general method not the thread.
java.awt and java.swing contain all of the classes for creating user
interfaces.
The methods of java.awt package are native methods. They are inherited from C or C++ package. C in platform dependent. the components created by these methods are also dependent on a platform. Example: the button in Windows OS in not same as the button create in the Unix operating system.
java.awt package is a heavy weight. It takes more resources to execute these methods.
java.swing uses less memory and it is platform independent.
معمولاً برای ارتباط با دیتابیس یا JDBCODBCBridDriver استفاده می شود و یا Oracle Thin Driver
در صورتی که از JdbcOdbcDriver استفاده شود، نام کلاسی که برای رجیستر کردن درایور استفاده می شود sun.jdbc.odbc.jdbcodbcDriver است و URL ای هم که در متد getConnection استفاده می شود jdbc:odbc:mydsn است.
در صورتی که از Oracle thin Driver استفاده شوذ، نام کلاسی که برای رجیستر کردن درایور استفاده می شود Oracle.jdbc.driver.OracleDriver است و URL ای هم که در متد getConnection استفاده می شود jdbc:oracle:thin:@localhost:152:0rcl است. لازم به ذکر است که اطلاعاتی که بعد از @ نوشته شده است در هر سیستم متفاوت است و این اطلاعات از روی فایل tsnname اوراکل که در فولدر network->Admin در جایی که اوراکل نصب شده است، به دست می آید.
به جز نام کلاس و URL که در فوق به آن اشاره شد، سایر مراحل اتصال به دیتابیس در هر یک از دو روش فوق یکسان می باشد.
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");
Java is good in processing data and database is good in storing data. Java understands only Java programming language and database knows only structure body language.
Java API supports SQL package using which we can made a connection among the java programming language and the database. For making a connection we have to follow 5 different steps:
Step1: Registration Drive: there a 4 different types of driver. JDBCODBCBridDriver, Native API Partly Java Driver, NetProtocolPureJava Driver and Oracle thin Driver