First: connect to the database through ODBC
JAVA's cross-platform processing capabilities (Write Once, Run Anywhere) and excellent image processing capabilities (I believe there is no such language that can surpass JAVA's network processing capabilities) network communication functions, JDBC database access technology, and so on, it is undeniable that the JAVA language is SUN's great contribution to the computer industry. I can describe this scenario: one day you can access the Internet without using IE or NETSCAPE. surfing the internet is like playing a game, and you can get the beautiful images and interactions of the game, if you have played UO, you may know that feeling, but JAVA will definitely make more things than UO, because it is not just a game, it is not just a browser, if you want to (if you want to have money, time, and excellent JAVA talents), you can fully integrate all this with Java !!! I'm not exaggerating the JAVA feature, you can visit the Community Program of http://www.simchina.net, you can find a feeling: Believe I Didn't Say Anything false. Now I will introduce you to JAVA's database access technology-JDBC database access technology (you should never turn it into ODBC !).
In fact, JDBC is an application interface (API) that can access any structured database using the JAVA language (I don't know whether it is true, as Sun said ), in addition, JDBC 3.0 can also access spreadsheet programs such as Execel, according to Sun!
There are four methods for JDBC to access the database. Here we only introduce two methods:
The first is to access the database through ODBC as the Bridge, and the second is to access the database directly.
Let's take a look at the first JDBC <--> ODBC access process:
JDBC Driver Mannager-> JDBC <-> ODBC bridge-> ODBC-> database client Driver library-> database server-> return query results, in this way, we note that although JAVA is "Write Once, Run Anywhere", if this access is used, the client must set ODBC and the driver of the corresponding database client, when you read the other process below, you may think: why is there a more obvious problem! Well, because not all database server providers provide the following JDBC drivers (providing corresponding interfaces for JDBC access), JDBC <-> ODBC Bridge is available.
Next let's take a look at the second access process:
JDBC Driver Mannager-> Local JDBC Driver-> client database-> database server-> return query results. In fact, this access is converted to the corresponding database (Oracle, Sybase, Informix, DB2, and other database management systems) Client API call (so, I don't know if you can understand it. To put it simply, ASP does not access the database through DSN but through OLEDB, I still don't know if you can understand what I mean. Oh, don't throw eggs !), This access method requires the corresponding database provider to provide the corresponding JDBC driver, but there is a benefit that odbc can be used independently in the browser Applet program of clients that can Run anywhere.
We will give you an example of access through the JDBC-ODBC bridge database, but before looking at the example below I want to ask you once: JDK1.3 installed? Is the database Driver Installed (I am using SQLserver )? Shouldn't you use Linux? Although java supports Linux, I didn't use Linux, dude (this is irrelevant to JAVA's Write Once and Run Anywhere), because ODBC running under Win is used, I suggest you take a look at this http://www.aspcn.com/showarticle.asp? Id = 112. Otherwise, if you have a problem and you cannot get the result, it's not a blame for me (but it's just a sin to add, why don't you eat it!
So much nonsense, let's take a look at the JDBC call! Since we access the database through odbc, this odbc cannot run. Let's first set your odbc: Open your odbc Data Source-> select the system dsn (Click the new dsn -) -> next, enter "select database type", "Enter dsn name", "server", "connection method", and "Login User and password".-> test the connection. If the test succeeds, then you have created your dsn. My dsn name is Sqlserver. use sqlserver7.0 and log on with "sa". The password is blank. These things are all coming soon!
Now let's look at the program code: (the code has been run)
//###################################### #####################
// Code starts
Import java. SQL .*;
// Load the java data connection package. This is where java basically calls all databases.
Public class InsertCoffees {
Public static void main (String args []) {
String url = "jdbc: odbc: sqlserver ";
// Get the url name of the connection. Note that dsn is the dsn name.
Connection con;
// Instantiate a Connection object
Statement stmt;
String query = "select * from col_link ";
// Select data output from all Col_link tables
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Load the jdbc-odbc bridge driver
} Catch (java. lang. ClassNotFoundException e ){
System. err. print ("ClassNotFoundException :");
// Jdbc-odbc bridge loading error
System. err. println (e. getMessage ());
// Other errors
}
Try {
Con = DriverManager. getConnection (url, "sa ","");
// Database connection
Stmt = con. createStatement ();
// Create a statement
Stmt.exe cuteUpdate ("create table col_link (sitename varchar (20) NULL, siteurl varchar (50) NULL )");
// Execute an SQL statement to generate a table named col_link.
Stmt.exe cuteUpdate ("insert into col_link values ('asp China', 'HTTP: // www.aspcn.com ')");
Stmt.exe cuteUpdate ("insert into col_link values ('How far is there forever? ', 'HTTP: // xuankong.com ')");
// Execute an insert into statement
Stmt.exe cuteUpdate ("update col_link set siteurl = 'HTTP: // www.aspcn.com/xuankong/xuankongt.jpg' where siteurl = 'HTTP: // xuankong.com '");
// Execute an update statement to update the database
ResultSet rs = stmt.exe cuteQuery (query );
// Returns a result set.
System. out. println ("the data in the Col_link table is as follows (raw data )");
// The following statement uses a while loop to print all the data in the col_link table
System. out. println ("site name" + "" + "site address ");
System. out. println ("---------------" + "" + "----------------");
While (rs. next ()){
String s = rs. getString ("sitename ");
String f = rs. getString ("siteurl ");
// Obtain data in the database
System. out. println (s + "" + f );
/* String t = rs. getString (1 );
String l = rs. getString (2 );
System. out. println (t + "" + l );*/
/* Jdbc provides two methods to identify fields. One is to use getXXX (note that getXXX indicates different methods for obtaining different types of fields) to obtain the field name,
The second method * is through field index. Here I comment out the second method */
/* You can access this connection to get getxxx usage: http://java.sun.com/docs/books/tutorial/jdbc/basics/_retrievingtable.html */
}
Stmt. close ();
Con. close ();
// Close the Declaration and connection in the preceding statement
} Catch (SQLException ex ){
System. err. println ("SQLException:" + ex. getMessage ());
// Display database connection error or query Error
}
}
}
// Code ended