1 Packagecom.cps.rom.utils;2 3 /*******************************************************************************4 * Test program to connect to MS SQL Server 2000 database using Java Pure driver5 * Yulaw6 * Date: 2005-11-067 ******************************************************************************/8 9 /*******************************************************************************Ten * Preparatory work: One * 1. To connect a SQL Server 2000 database using a Java-only driver, you must first obtain a driver from the database vendor A * program. - * 2. Copy the Jdbcsqlserver directory in the Dbdriver directory to the local disk. - * 3, set the CLASSPATH in the environment variable, specify the path of the driver. The driver has a total of three. jar files, the * The path to the three files must be specified. - The path to the above file should also be specified in the development environment for writing Java programs (e.g., JCreator, etc.). - ******************************************************************************/ - //to connect to a database, you must include the java.sql package + - Public classTest { + Public Static voidMain (String args[]) { ASYSTEM.OUT.PRINTLN ("Connecting to database, please wait ..."); at Try { - //First step: Registering the JDBC driver - /* - * If you are connecting to SQL Server in a pure Java-driven manner, use the following string as fixed: - * "Com.microsoft.jdbc.sqlserver.SQLServerDriver" cannot be changed - */ in //the Forname () method has the potential to throw classnotfoundexception exceptions and must capture - //java.lang.Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"); toClass.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); + //Step Two: Connect to the database - /* the * To set the connection string, the following format should be used: * * "jdbc:microsoft:sqlserver://server name or IP address: port number (default 1433);d atabasename= database name" $ */Panax Notoginseng //connect to the pubs database of SQL Server here - //String Strcon = the //"Jdbc:microsoft:sqlserver://Localhost:1433;databasename=db_wuliu "; + AString Strcon = "Jdbc:sqlserver://localhost:1433;databasename=db_wuliu"; the +String strUserName = "sa";//user name of the database -String strpwd = "123";//password for the database $ //establishing a database connection $ //the Getconnection () method is a static method of the DriverManager class - //the Getconnection () method has the potential to throw SqlException exceptions and must capture -Java.sql.Connection con =java.sql.DriverManager.getConnection (Strcon, strUserName, strpwd); theSystem.out.println ("Successfully connected to the database. "); - Wuyi //Step Three: Create a statement object handle using the connection established above theJava.sql.Statement STA =con.createstatement (); -Sta.setquerytimeout (30);//set operation delay to 30 seconds Wu - //Next, manipulate the database using the statement object handle created above AboutString strquery = "SELECT * from Db_customer";//Query Statements $Java.sql.ResultSet rs = sta.executequery (strquery);//executes a query statement, returning a recordset - intCount = 0;//counter -SYSTEM.OUT.PRINTLN ("Data queried:"); - while(Rs.next ()) { AString strFirstName = rs.getstring ("Name");//get data for the specified field +String strLastName = rs.getstring ("Email"); thecount++;//Counter Count -System.out.println (strFirstName + "." + strLastName);//print the data from the query $ } theSYSTEM.OUT.PRINTLN ("Total query to" + Count + "row data.) ");//How many data does the statistic query the the //after the database operation is complete, close all open resources theRs.close ();//To close a record set -Sta.close ();//closing a statement object handle inCon.close ();//Close Connection theSYSTEM.OUT.PRINTLN ("The database is closed. "); the } About //catching exceptions and handling them the Catch(java.lang.ClassNotFoundException cnfe) { the System.out.println (Cnfe.getmessage ()); the}Catch(java.sql.SQLException se) { + System.out.println (Se.getmessage ()); - } the }Bayi}
Java Connection SQL Server