![](/img/trans.png)
[英]connecting to sql server 2014 database from basic java application?
[英]Transact SQL Statements from java to Ms SQL Server 2014
早上好,开发人员(今天上午在肯尼亚)。 我一直在使用Java和MySql数据库。 我的最新客户希望我根据他当前安装的数据库(运行Microsoft Sql Server 2014 Enterprise Edition)创建一个解决方案。 下面的代码连接到SQL Server实例,但不能处理任何SQL语句。 我收到SQL异常“无效的对象名称”。 请帮我解决代码。 谢谢。
private java.sql.`enter code here`Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName = "localhost\\PHILIP";
private final String portNumber = "1433";
private final String databaseName = "Payrol";
private final String userName = "sa";
private final String password = "kukaphilip";
public Main() {
System.setProperty("java.net.preferIPv6Addresses", "true");
getConnection();
}
private String getConnectionUrl() {
//System.out.println(java.lang.System.getProperty("java.library.path"));
return "jdbc:jtds:sqlserver://localhost;Database=Payrol;integratedSecurity=true";
}
private java.sql.Connection getConnection() {
try {
/*Class.forName("net.sourceforge.jtds.jdbc.Driver");*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(getConnectionUrl());
if (con != null) {
System.out.println("Connection Successful!");
String sql = "INSERT INTO payrol.dbo.test values('1','Philip')";
Statement st = con.createStatement();
st.executeUpdate(sql);
/*st.setInt(1, 1);
st.setString(2, "Philip");
st.execute();*/
}else{
System.out.println("Connection failed");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
检查这些
1)尝试在SSMS中执行相同的查询,看看是否出现任何错误。
2)将payrol.dbo.test放入方括号内,例如[payrol]。[dbo]。[test]
3)如果排序规则区分大小写,则“ test”的大小写在数据库和查询中必须相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.