SQL Server 2000 is today's network programming in the use of more than a database system, while Java is a popular network programming language, in the network programming involved in more and more important is the database problem, Java itself provides a variety of mainstream database system support, By providing the java.sql library, provides a unified interface, so that you can in the Java environment do not have to make large-scale changes to the program, as long as the corresponding driver changes, you can achieve the operation of various databases, thereby improving the software lifecycle and reduce software development costs and maintenance costs, In this case, the integration of SQL Server 2000 and Java is more important, and this article briefly describes the integration of SQL Server 2000 and JDK.
System Requirements:
Database: SQL Server SP3
Driver: SQL Server driver for JDBC
Java version: jdk1.2 above
1, we first use the Jdbc-odbc Bridge to implement the database connection, this relatively simple, first use SQL Server Enterprise Management it to build a database test, and establish a simple table first_table ODBC data source, each step with the default action.
Write a simple test program that mainly implements the database connection, and a simple SQL operation, the code is as follows:
/***********************************************
/*
/*dbtest.java
/*
/******************************************* */
Import java.sql.*;
public class Dbtest
{
Connection con;
Statement STA;
ResultSet rs;
String driver;
String URL;
String user;
String pwd;
Public Dbtest ()
{
Driver = "Sun." JDBC.odbc.JDBCOdbcDriver ";
url = "JDBC:odbc:store_manager";
user = "Share";
PWD = "Share";
Init ();
}
public void Init ()
{
try{
Class.forName (driver);
System.out.println ("Driver is OK");
con = drivermanager.getconnection
(URL,USER,PWD);
System.out.println ("conection is OK");
STA = Con.createstatement ();
rs = Sta.executequery
("SELECT * from room");
while (Rs.next ())
System.out.println
(Rs.getint ("Roomnum"));
}catch (Exception e)
{
E.printstacktrace ();
}
}
public static void Main
(String args [])
Replace yourself []
{
New Dbtest ();
}
}