Java数据库与SQL Server的连接。 [英] Java Database Connectivity to SQL Server.

查看:74
本文介绍了Java数据库与SQL Server的连接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有帐户模块的Web应用程序。我可以使用以下连接和查询成功登录。我的TCP / IP也是启用及其属性AP Adddress - > IPAll - > TCP端口设置为1443

I am creating a web application with a account module. I can successfully login using the below connection and query. Also my TCP/IP is enable and its properties AP Adddress -> IPAll -> TCP Port is set to 1443

public class ConnectionDAO {
    protected Connection conn = null;
    protected PreparedStatement stmt = null;
    protected ResultSet result = null;

    public ConnectionDAO() {
        try {
        	Configuration config = new Configuration();
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			this.conn = DriverManager.getConnection("jdbc:sqlserver://" + config.getUrl() + ":1443;databaseName=" + config.getDatabase(), config.getUsername(), config.getPassword());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
}

public class LoginDAO extends ConnectionDAO {
	
	public LoginDAO() {
		super();
	}

        public UserBEAN getLogin(String username, String password) {
		UserBEAN login = new UserBEAN();
		try {
			result = conn.createStatement().executeQuery("SELECT * FROM [tblLogin] WHERE [username] = '" + username + "'; ");
			while (result.next()) {
				login.setId(result.getInt("ID"));
				login.setUsername(result.getString("username"));
				login.setPassword(result.getString("password"));
				login.setType(result.getInt("type"));
				login.setEncrypted(result.getBoolean("encrypted"));
				if (login.isEncrypted()) {
					login.setPassword(StringCrypt.decypt(login.getPassword(), Global.KEY));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return login;
	} 
}





但是当我使用prepareStatement在数据库上插入新用户时出现此错误。





But when I'm using prepareStatement to INSERT new user on database I got this error.

<br />
The TCP/IP connection to the host :1443, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".<br />





我可以成功创建结果集,但prepareStatement不是。请帮助谢谢



I can successfully create a resultset but prepareStatement is not. Please Help Thanks

推荐答案

SQL Server默认端口是1433,而不是1443.您的连接字符串中有拼写错误。



此外,你在try..catch块中使用的 Class 变量是什么?不应该是 config 变量吗?

错误消息似乎表明 config.getUrl( )方法返回一个空字符串。



调试会话不会没用。
SQL Server default port is 1433, not 1443. You're having a typo in your connection string.

Moreover, what is the Class variable you are using in your try..catch block? Shouldn't it be the config variable here instead?
The error message seems to indicate that the config.getUrl() method is returning an empty string.

A debugging session would not be useless.


这篇关于Java数据库与SQL Server的连接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆