JDBC的JDBC示例 [英] JDBC Example for java
问题描述
我已经下载了JDK 6,而且我有sqljdb4.jar,我有database.properties文件,其中包含以下数据
I have downloaded JDK 6 and also I have sqljdb4.jar and I have database.properties file that content the following data
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true;
database.username=sa
database.password=admin
BN:我在我的服务器上安装服务器机器和服务器名称=。 ,我也在使用Windows Authontication
B.N : I'm installing the server on my machine and the server name = . , also I'm using Windows Authontication
我现在的问题是当我尝试创建连接时出现以下错误
My problem now is when I try to create connection I have the following error
com.microsoft.sqlserver.jdbc.SQLServerException:
与主机
localhost,端口1433的TCP / IP连接失败。
错误:连接被拒绝:连接。
请验证连接
属性并检查主机上是否正在运行SQL Server
实例,而
端口是否接受TCP / IP连接的
,并且没有防火墙阻止
到端口的TCP连接。 at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: Connection refused: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
我不知道知道这里的确切问题是什么
I don't know what is the exact problem here
如果有人能提供帮助我将不胜感激
If any one can help I will be appreciated
在此先感谢
推荐答案
这是由许多概率引起的,例如
1- IP是worong
2-端口错误
3-有防火墙阻止机器外出并连接到另一个IP
4- SQL服务器。
That's caused by many probabilities like 1- IP is worong 2- Port is wrong 3- There is firewall prevent machine to go out and connect to another IP 4- SQL server down .
尝试使用
public class JdbcSQLServerDriverUrlExample
{
public static void main(String[] args)
{
Connection connection = null;
try
{
// the sql server driver string
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// the sql server url
String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
// get the sql server database connection
connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
// now do whatever you want to do with the connection
// ...
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
我需要解释的是有一种非常好的技术称为持久性,它比JDBC更好,并且非常出色且易于使用。
What i need to explain is there is very good technology called " Persistence " is better than JDBC and is more than brilliant and easy to use .
这篇关于JDBC的JDBC示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!