Java implementation connects SQL Server 2000_mssql

Source: Internet
Author: User
Tags dsn getmessage stmt
First: Connecting to the database through ODBC

The cross-platform capabilities of the Java language (Write Once, Run Anywhere), excellent image processing capabilities (I believe there is no such language that can exceed Java's graphics processing capabilities on the network), network communications, access to the JDBC database, and so on, Let us no one can deny that the Java language is a great contribution to the computer world by Sun. The author can describe such a scene: One day you can go online without IE or Netscape, Internet is like playing games, you can get the game so exquisite image and interactive feeling, if you play uo, maybe you know that feeling, but Java made things will be more than UO, Because it's not just a game, it's not just a browser, if you want to (you're rich, you have time, you have good Java talent) You can completely integrate all of this in Java!!! I am not exaggerating the functionality of Java, you can visit the Http://www.simchina.net Community program, you can find a feeling: Believe I did not say what lies. Okay, no more nonsense, now I'm going to introduce Java database access technology----JDBC Database access technology (you mustn't make ODBC!) )。
JDBC technology is in fact an application interface (API) that accesses any structured database through the Java language (as Sun says, I don't know if it is true), and now the JDBC 3.0, according to Sun, can also access spreadsheet programs like Execel!
JDBC has four ways to access the database, and we only introduce two of them:
The first is access to the database through ODBC as bridge, and the second is direct access to the database.
Let's take a look at the first type of JDBC<-->ODBC access process:
JDBC Driver MANNAGER-&GT;JDBC&LT;-&GT;ODBC Bridge->odbc-> Database client drive Library-> database server-> return query results in which the value of our attention is although Java is " Write Once, Run Anywhere, but if this access is required, the client must set up ODBC and have the corresponding database client driver, and when you look at the other process below you may think: Clearly the next more, why have this thing to produce! Hehe, because not all database server providers provide the following JDBC driver (providing the appropriate interface to JDBC access), so there's Jdbc<->odbc bridge.
And then let's take a look at the second type of access flow:
JDBC Driver mannager-> Local JDBC driver-> client Database-> database server-> Returns the query result, which is in fact converting JDBC calls to the corresponding database (Oracle, Sybase, Client API calls for Informix, DB2, and other database database management systems (so, don't know if you can understand, say simple point as if ASP is not accessed through DSN to the database but through OLE DB), I still don't know if you can understand what I mean. Oh, don't throw eggs! This way of access requires the appropriate database provider to provide the appropriate JDBC driver, but there is a benefit that can be independent of the applet program in the browser of the client that can be run anywhere.
We will give you an example of a JDBC-ODBC bridge database, but before looking at the following example I would like to ask you once: JDK1.3 installed? is the database driver installed (I'm using SQL Server)? You're not using Linux, are you? Although Java supports Linux, but I'm not using Linux (which has nothing to do with Java's write Once, run anywhere), I suggest you take a look at this article because of the use of ODBC, which runs under win http:// www.aspcn.com/showarticle.asp?id=112, or if you have a problem, not out of the results that is not to blame me (but want to add sin, what suffering without eating ...) ), wronged Ah!

Gee, say so much nonsense, or let us take a look at the end of the JDBC call it! Since we are accessing the database through ODBC, so this ODBC is not run, we first set your ODBC: Open your ODBC data source-> Select the System DSN (click Add New dsn-)-> then enter the Select database type, enter the DSN name:, Select the server, the way to connect the database, enter the database login user and password-> test the connection, if the test succeeds, then your DSN is established, my DSN name is SQL Server. Use sqlserver7.0 to log in as "sa" and the password is empty. These things have to be used in the back of the road!
Okay, let's take a look at the program code: (The code is already running)
//###########################################################
Code start
Import java.sql.*;
Load Java Data connection package, Java basic All the database calls are in this thing
public class Insertcoffees {
public static void Main (String args[]) {
String url = "Jdbc:odbc:sqlserver";
Get the URL name of the connection, note that SQL Server is the DSN name
Connection con;
Instantiate a Connection object
Statement stmt;
String query = "SELECT * from Col_link";
Select the data output from all Col_link tables
try {
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Load JDBC-ODBC Bridge Driver
catch (Java.lang.ClassNotFoundException e) {
System.err.print ("ClassNotFoundException:");
Load JDBC-ODBC Bridge Error
System.err.println (E.getmessage ());
Other errors
}
try {
con = drivermanager.getconnection (URL, "sa", "");
Database connection
stmt = Con.createstatement ();
Create a declaration
Stmt.executeupdate ("CREATE TABLE col_link (sitename varchar () NULL, SiteURL varchar ())");
An SQL statement was executed to generate a table col_link the table
Stmt.executeupdate ("INSERT INTO Col_link values" (' ASP China Net ', ' http://www.aspcn.com ') ");
Stmt.executeupdate ("INSERT into col_link values (' Forever how far ', ' http://xuankong.com ')");
Executes an INSERT INTO statement
Stmt.executeupdate ("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.executequery (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 out 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");
Get the 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 for identifying fields, one for obtaining field names using getxxx (note that here GetXXX represents different methods for taking different types of fields).
The second type is indexed by the field, where I annotate the second method.
/* You can access this connection to obtain GETXXX usage: http://java.sun.com/docs/books/tutorial/jdbc/basics/_retrievingtable.html*/
}
Stmt.close ();
Con.close ();
The statement above closes the declaration and the connection
catch (SQLException ex) {
System.err.println ("SQLException:" + ex.getmessage ());
Display database connection errors or query errors
}
}
}
End of code
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.