AP Plet is a small application written in the Java language that can be embedded in HTML and interpreted by the WWW browser. But how do you handle the vast amounts of data in the Internet world and the various resources that are distributed across the Internet in AP Plet? This is going to use JDBC.
First, the working principle of JDBC
JDBC (Java DataBase connectivity) is a Java application interface for executing SQL statements, consisting of a set of classes and interfaces written in the Java language. JDBC is a specification that enables database vendors to provide standard database access classes and interfaces for Java programmers, making it possible to develop tools and products that are independent of DBMS Java applications. JDBC uses the Jdbc-odbc Bridge to access the database through ODBC.
Ii. jdbc Method of writing database program
1. Establishing a data source
Establishing a data source means establishing an ODBC data source.
2. Establish a connection
The standard way to establish a connection with a database is to invoke method Drivermanger.getconnection (String url,string user,string password). The Drivermanger class is used to process driver transfer and support for new database connections.
3. Execute SQL statement
JDBC provides a statement class to send SQL statements, and objects of the statement class are created by the Createstatement method; After the SQL statement is sent, the result returned is usually stored in an object of the ResultSet class, ResultSet can be considered a table that contains the column names and corresponding values returned by SQL, and a pointer to the current row is maintained in the ResultSet object. Through a series of getxxx methods, you can retrieve the columns of the current row and display them.
Implementation of JDBC Programming database program
To facilitate the description of the problem, make the following assumptions. Operating Environment: Windows 98 (Additional personal Web Server (PWS)), IE4.0 or above browsers, ACCESS97, program editing, compilation environment: vj++6.0.
1. Set up a data source Interweb, the database connected by the use of Access 97 established interweb, the table is T_interdata, the structure is as follows:
Field Name Type length
BH Text 10//number
MC text 20//Name
DJ Currency Auto//Unit Price
2. Implementation procedures
Use vj++6.0 to establish AP Plet applet AP Plet1.java, and modify the code as follows: Import java.awt. * ;
Import Java.ap Plet. * ;
import java. sql. * ;
public class AP Plet1 extends AP Plet
{
public void Init ()
{
Resize (400, 300);
}
public void Paint (Graphics g)//This method is used to display output
{
this. SetBackground (Color.lightgray); Define background color
this. Setforeground (color.red); Define foreground color
String url = "Jdbc:odbc:interweb";
String ls_1 = "SELECT * from T_interdata";
Try//Exception handling module
{
Class.forName ("Com.ms.jdbc.odbc.JdbcOdbcDriver"); Load Driver
Establish a connection
Connection con = drivermanager.getconnection (URL, "sa", "");
Execute SQL
Statement stmt = Con.createstatement ();
ResultSet result = Stmt.executequery (ls_1); return results
g.DrawString ("Numbering", 40, 40);
g.DrawString ("name", 80, 40);
g.DrawString ("value", 160, 40);
int i = 10;
while (Result.next ())
{
Take the values of each field
g.DrawString (result.getstring (1), n, + i);
g.DrawString (Result.getstring (2), n, + i);
g.DrawString (Result.getstring (3), 160, + i);
i + 20;
}
Close connection
Result.close ();
Stmt.close ();
Con.close ();
}
Catch exception
catch (SQLException ex) {}
catch (Java.lang.Exception ex) {}
}
}