Connecting SQL Server 2000 with Java

The first: the cross platform ability of Java language to connect the database through ODBC (write once, run anywhere), excellent image processing ability (I believe no language can exceed Java's graphics processing ability on the network), network communication function, JDBC database access technology, etc, None of us can deny that Java language is a great contribution of Sun company to the computer industry. The author can describe such a scene: one day you can surf the Internet without IE or Netscape. Surfing the Internet is like playing games. You can get such beautiful images and interactive feeling of the game. If you have played uo, you may know that feeling, but what Java makes will surpass uo, because it is not just a game, nor a browser, If you are willing (want you to have money, time and excellent Java talents), you can fully integrate all these with java!!! I am not exaggerating the functions of Java, you can visit it http://www.simchina.net You can find a feeling: believe me, I'm not lying. Well, don't talk nonsense. Now I'll introduce you to Java's database access technology - JDBC database access technology (don't make it ODBC!). JDBC technology is actually an application program interface (API) that can access any structured database through Java language (I don't know if it's true), And now JDBC 3.0, according to sun, can also access excel and other spreadsheet programs! There are four ways for JDBC to access the database. Here we only introduce two ways: the first is to access the database through ODBC as a "bridge", and the second is to access the database directly. Let's take a look at the first JDBC < -- > ODBC access process: JDBC Driver Manager - > JDBC < - > ODBC bridge - > ODBC - > database client driver library - > database server - > return query results. In this access value, we should note that although Java is "write once, run anywhere", if this access is used, ODBC and the corresponding database client driver must be set on the client. When you look at another process below, you may think: why should this thing be produced in the next aspect! Hehe, because, Not all database server providers provide the following JDBC drivers (provide corresponding interfaces for JDBC access), so there is JDBC < - > ODBC bridge. Then let's take a look at the second access process: JDBC Driver Manager - > local JDBC Driver - > client database - > database server - > return query results. This access actually converts JDBC calls to corresponding databases Client API calls for Oracle, Sybase, Informix, DB2, and other database management systems (so, I don't know if you can understand it. To put it simply, it's like that ASP accesses the database not through DSN but through OLEDB. I still don't know if you can understand what I mean here. Oh, don't throw eggs!), this way of access requires the corresponding database provider to provide the corresponding jdbc driver, but it has one advantage, which can be used alone Applet program in ODBC browser for clients that can run anywhere. We will give you an example of database access through jdbc-odbc bridge, but before looking at the following example, I want to ask you once: jdk1 Did you install it? Is the database driver installed (I use sqlserver)? You shouldn't use Linux? Although Java supports Linux, man, I don't use Linux (this has nothing to do with write once and run anywhere in Java). Since ODBC running under win is used, I suggest you take a look at this article http://www.aspcn.com/showarticle.asp?id=112 Otherwise, if you have a problem, If you can't get the result, don't you blame me (but if you want to add a crime, why don't you have to eat...), wronged! Alas, after all this nonsense, let's see what JDBC calls are! Since we access the database through ODBC, this ODBC can't run. Let's set up your ODBC first: open your ODBC data source - > select system DSN (click to add a new DSN -) - > next, enter and select the database type, enter the DSN Name:, select the server, the way to connect to the database, enter the login user and password of the database - > test the connection. If the test is successful, your DSN will be established, and my DSN name is sqlserver SQL Server 7.0 is used 0, log in as "Sa", and the password is blank. These things are used in the back! Well, well, the following is the code code of the following let let let us look at the program code of the following following following the well well. The code of the program code of the following let let let let let let let let let let let let the following following the following the the code code of the program code of the code code code of the program code of the following following the following the the following the following the the following the the following the following the following the the following the following the following the well well well well well well well well well, the following let let let let let let let let let let let let let let the following the code code code of the following the code code code code code code of the following following the following the following the following the code code code of the following following the following the following the code code code of the following the following the code code code code of the following the following the code code code code of the following the following the following the following the code code code code code code code code of the following the following the following the following the following the following the following the following the following the following the following the following the following the following the following the############################ / / the code starts to import java.sql.; / / load the Java data connection package, which basically calls all Java databases. Public class insertcoffees {public static void main (string args []) {string url = "JDBC: ODBC: sqlserver"; / / get the URL name of the connection. Note that sqlserver is DSN connection con; / / instantiate a connection object statement stmt; string query = "select from col_link"; / / select all the data in the col_link table and output try {class. Forname ("sun. JDBC. ODBC. Jdbccodbcdriver"); / / load the JDBC ODBC bridge driver} catch (Java. Lang. classnotfoundexception E) {system.err.print ("classnotfoundexception:"); / / loading 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 (20) Null, siteurl varchar (50) null) "; / / an SQL statement is executed to generate a table stmt.executeupdate (" insert into col_link values ('asp Chinanet ',' http://www.aspcn.com '"); stmt. Executeupdate (" insert into col_link values ('How far is forever', ' http://xuankong.com '"); / / execute 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); / / return a result set system.out.println (" the data in col_link table is as follows (original data) "); / / the following statement uses a while loop to print out all the data in 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 to identify fields. One is to use getxxx (note that getxxx here represents different methods for obtaining different types of fields) to obtain field names. The other is to use field indexes. Here I annotate the second method / / you can access this connection to obtain the usage of getxxx: http://java.sun.com/docs/books/tutorial/jdbc/basics/_retrievingTable.html */ } stmt. close(); con. close(); // The above statement closes the declaration and connection} catch (sqlexception Ex) {system. Err. Println ("sqlexception:" + ex.getmessage()); / / a database connection error or query error is displayed}}} / / the code ends

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>