Java test code to connect to the database
1 ImportJava.sql.*;2 3 Public classConmanager {4 Final StaticString CFN = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";5 Final StaticString url = "Jdbc:sqlserver://localhost:1433;databasename=student";6 7 Public Static voidMain (string[] args) {8Connection con =NULL;9PreparedStatement statement =NULL;TenResultSet res =NULL; One Try { A Class.forName (CFN); -con = drivermanager.getconnection (URL, "sa", "1234"); - theString sql = "Select *from test";//Query Test Table -statement =con.preparestatement (SQL); -res =statement.executequery (); - while(Res.next ()) { +String title = res.getstring ("Test_name");//gets the elements of the Test_name column ; -System.out.println ("Name:" +title); + } A at}Catch(Exception e) { - //Todo:handle Exception - e.printstacktrace (); -}finally{ - Try { - if(Res! =NULL) Res.close (); in if(Statement! =NULL) Statement.close (); - if(Con! =NULL) Con.close (); to}Catch(Exception E2) { + //Todo:handle Exception - e2.printstacktrace (); the } * } $ }Panax Notoginseng}
To import the Sqljdbc4.jar package in a Java project
Project name right mouse button->build path->configure Build Path
Add Sqljdbc4.jar
Add success
The next step is to process the SQLSERVER2012 database
1. Ensure the normal start of service
2. Ensure that these are available in the database
Running a Java project
Database connection Success!!!
JAVA JDBC Connection SQLServer2012