import
java.sql.*
;
public
class
SqlServerTest {
//
驱动类
//
static String driverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
static
String driverClass = "com.mysql.jdbc.Driver"
;
//
连接字符串
//
static String url = "jdbc:microsoft:sqlserver:
//
HNHJ\\HNHJ2:1433;dataBaseName=db_net";
static
String url = "jdbc:mysql://127.0.0.1:3306/db_net?characterEncoding=utf8&autoReconnect=true"
;
//
密码
//
static String password = "sa";
static
String password = "test"
;
//
用户名
//
static String username = "";
static
String username = "test"
;
//
待执行的 SQL 语句
static
String sql = "SELECT * FROM tb_news"
;
public
static
void
main(String[] args) {
Connection conn
=
null
;
PreparedStatement pstmt
=
null
;
ResultSet rs
=
null
;
try
{
Class.forName(driverClass);
conn
=
DriverManager.getConnection(url, username, password);
pstmt
=
conn.prepareStatement(sql);
rs
=
pstmt.executeQuery();
while
(rs.next()) {
System.out.println(
"OK."
);
}
System.out.println(
"OK too."
);
rs.close();
pstmt.close();
conn.close();
}
catch
(ClassNotFoundException e) {
System.out.println(
" 驱动类没有找到 ."
);
e.printStackTrace();
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
if
(rs !=
null
)
//
结果集没有关闭时关闭结果集
try
{
rs.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
if
(pstmt !=
null
)
//
发送对象没有关闭时关闭发送对象
try
{
pstmt.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
if
(conn !=
null
)
//
连接没有关闭时关闭连接
try
{
conn.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
}
java.sql.SQLException : [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
--查看端口号及测试连接--
方法:Microsoft SQL Server ->SQL Server 组 ->选择数据库(如:HNHJ\HNHJ2)
->属性 ->常规 ->网络配置 ->TCP/IP属性 ->查看默认端口号。
Cannot create JDBC driver of class '' for connect URL 'null'
--更换数据源连接方式-- 避免Tomcat管理数据源。

