Package com. gamvan. Conn;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. statement;
Public class conntest {
Private string gvdriver = "";
Private string gvuser = "sa"; // database access user
Private string gvpassword = "123"; // Database Access Password
Private Static string message = "congratulations, the database connection is normal! ";
Public connection getconnection (){
Connection conn = NULL;
Try {
Class. forname ("net. SourceForge. jtds. JDBC. Driver ");
/**
* The database driver jtds needs to download the jtds jar package and place it in the lib directory of the software runtime environment.
* Db_test indicates the database name.
*/
Gvdriver = "JDBC: jtds: sqlserver: // 127.0.0.1: 1433; databasename = db_test ";
Conn = drivermanager. getconnection (gvdriver, gvuser, gvpassword );
} Catch (exception e ){
Conn = NULL;
Message = E. tostring ();
}
Return conn;
}
/**
* Set the closing method.
*/
Public void close (connection c ){
Try {
If (C! = NULL) C. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
/* Test database connection */
Public static void main (string ARGs []) {
Conntest Ct = new conntest ();
Connection con = CT. getconnection ();
// Print information
System. Out. println (Message );
// Good habits should be developed. Remember to close the database connection when it is used up
Ct. Close (CON );
}
}