Example of JDBC database access (2)

Source: Internet
Author: User

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:
//###################################### #####################
// 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 Zhonghua, http://www.aspcn.com )");
Stmt.exe cuteUpdate ("insert into col_link values (always how far, 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
//###################################### #####################


In the above procedure I Want you to demonstrate how to use a JDBC-ODBC to connect to a database, use SQL statements to generate a table, use the SELECT, INSERT, UPDATE statements to retrieve, INSERT, and UPDATE data in a table, how can I access Dongdong in the database through field names and field indexes! I hope you can learn something from the above Code!

Give full play to your imagination and imagine the end of JAVA. For example, you can use a database to create a chat room without a GUI, it feels like typing in a chat room in a DOS environment! Haha!

The last thing I need to talk about is the environment of the above program debugging: WIN2000, JDK1.3, MS SQLSERVER editing software: EDITPLUS 2.01a (this last thing is not nonsense, although some professional JAVA development tools have been developed for a long time, I suggest that JAVA Beginners use text software to develop JAVA programs)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.