Steps to connect SQLServer 2008 database with Java:
1. Go to Microsoft's official website to download jdbc and unzip it to get sqljdbc.jar and sqljdbc4.jar. Since JDK1.7 is used, sqljdbc4.jar is used.
2. Copy the file sqljdbc4.jar to the jdk directory/jdk1.7.0/jre/lib/ext.
Configure the system variable classpath variable path D:/Java/jdk1.7.0/jre/lib/ext/sqljdbc4.jar
Test procedure:
Copy the 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("SQL driver loaded successfully");
}
catch(Exception e){
System.out.println("SQL driver not found");
}
System.out.println("hello huuuu!!");
}
}
3. Start->Programs->sql server 2008->Configuration Tools->SQL Server Configuration Manager. Start the sql 2008 service. Click the sql server2008 network configuration node and select the "SQLserver Protocol" node.
Enable the tcp/ip protocol, set the IPALL TCP port in the IP address to 1433, and then go to the service to restart SQL Server (very important...).
Test procedure:
Copy the 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("SQL driver loaded successfully");
}
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 successful");
}
catch(Exception e){
System.out.println("Database connection failed"); }
}
}
Note the DatabaseName in the above program. You need to create a database named SQLTest in the database first. Then start connecting again.
Well, after the above configuration and testing, you can use Java to operate the database in the future.