Java connection to SQL Server 2008 database:
1. To Microsoft official download JDBC and decompression, get Sqljdbc.jar and Sqljdbc4.jar, because the use is JDK1.7, so use Sqljdbc4.jar,
2. Copy the file Sqljdbc4.jar to the JDK directory \jdk1.7.0\jre\lib\ext.
Configure system variable classpath variable path D:\Java\jdk1.7.0\jre\lib\ext\sqljdbc4.jar
Test program:
Copy Code code as follows:
Import java.sql.*;
public class t1{
public static void Main (String []args)
{
try{
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println ("Successfully loaded SQL Driver");
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("SQL driver not Found");
}
System.out.println ("Hello huuuu!!");
}
}
3. Start-〉 program-〉sql server 2008-〉 Configuration Tool-〉sql server Configuration Manager. Start the SQL 2008 service. Click on the SQL server2008 Network Configuration node and select the SQL Server Protocols node.
Enable the TCP/IP protocol, set the Ipall TCP port in the IP address to 1433, and then restart SQL Server in the service (very important ...) )。
Test program:
Copy Code code as follows:
Import java.sql.*; public class t2{
public static void Main (String []args)
{
try{
class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println ("Successfully loaded SQL Driver");
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("SQL driver not Found");
}
try{
connectioncon=drivermanager.getconnection "Jdbc:sqlserver://localhost :1433;
databasename=sqltest ", "sa", "123");
Statement stmt = con.createstatement (); System.out.println ("database connection succeeded");
}
catch (Exception e) {
System.out.println ("Database connection failed"); }
}
}
Note the databasename in the above program, you need to first create a database named Sqltest in the database. And then start the connection again.
OK, after the above configuration and testing, you can use Java to operate the database.