当前位置: 技术问答>java相关
怎样使Java连接到MS SQL Server?
来源: 互联网 发布时间:2015-06-11
本文导语: 最好能附上源码。谢谢! | package myprojects.linkmssserver; import java.io.*; import java.sql.*; import oracle.jdbc.driver.*; import oracle.sql.*; public class Linkmssserver { public static Connection con = null; public stat...
最好能附上源码。谢谢!
|
package myprojects.linkmssserver;
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class Linkmssserver {
public static Connection con = null;
public static void main(String args[]) {
try {
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433", "sa", "112233");
System.out.println("连接成功!");
} catch (SQLException e) {
System.out.println("error in retrieve data from news");
}
}
}
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class Linkmssserver {
public static Connection con = null;
public static void main(String args[]) {
try {
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433", "sa", "112233");
System.out.println("连接成功!");
} catch (SQLException e) {
System.out.println("error in retrieve data from news");
}
}
}
|
先建一个数据源,这里为DnsPubs,然后按下面连:
........
String userid = "user";
String password = "user";
// Register the driver with DriverManager
new sun.jdbc.odbc.JdbcOdbcDriver();
// Get a connection
Connection conn = DriverManager.getConnection("jdbc:odbc:DsnPubs",
userid, password);
当然还有其它几种方式,不过我不怎么熟。
........
String userid = "user";
String password = "user";
// Register the driver with DriverManager
new sun.jdbc.odbc.JdbcOdbcDriver();
// Get a connection
Connection conn = DriverManager.getConnection("jdbc:odbc:DsnPubs",
userid, password);
当然还有其它几种方式,不过我不怎么熟。
|
(1)register the driver
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SqlServerDriver());
(2)connect
DriverManager.getconnection(".......",user,passwd);
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SqlServerDriver());
(2)connect
DriverManager.getconnection(".......",user,passwd);