JAVA connection to sqlserver database test Code
Package COM. conndbtest; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. sqlexception; public class getconnectionsqlserver {public void getconnectionsqlserver () {string drivername = "com. microsoft. JDBC. sqlserver. sqlserverdriver "; string dburl =" JDBC: sqlserver: // localhost: 1433; databasename = uscsecondhandmarketdb "; // 1433 is the port, "uscsecondhandmarketdb" is the database name string username = "sa ";/ /Username string userpwd = "sa"; // password connection dbconn = NULL; try {class. forname (drivername ). newinstance ();} catch (exception ex) {system. out. println ("Driver loading failed"); Ex. printstacktrace ();} Try {dbconn = drivermanager. getconnection (dburl, username, userpwd); system. out. println ("successfully connected to the database! ");} Catch (exception e) {e. printstacktrace ();} finally {try {If (dbconn! = NULL) dbconn. close ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static void main (string [] ARGs) {getconnectionsqlserver getconn = new getconnectionsqlserver (); getconn. getconnectionsqlserver ();}}