java web app在部署到Tomcat上时拒绝连接到SQL数据库
因此,我面临以下问题。 我开发了一个web应用程序,它与SQL Server数据库有以下连接。(附db连接代码) 这更多的是配置或服务器问题。不是代码问题。java web app在部署到Tomcat上时拒绝连接到SQL数据库,java,spring-mvc,tomcat,sqldatasource,Java,Spring Mvc,Tomcat,Sqldatasource,因此,我面临以下问题。 我开发了一个web应用程序,它与SQL Server数据库有以下连接。(附db连接代码) 这更多的是配置或服务器问题。不是代码问题。 正如在我测试过的注释中提到的,如果var conn==null->结果为true。连接不会在任何地方被置空。请查看代码。然后,当从InteliJ调试器运行时,上述代码再次起作用。因此,最终找到了问题所在。 为什么它在我的IDE配置上工作: 1.我使用JavaJDK1.7作为java_主页 2.我使用的是旧版本的Tomcat作为构建 3.为了
正如在我测试过的注释中提到的,如果var conn==null->结果为true。连接不会在任何地方被置空。请查看代码。然后,当从InteliJ调试器运行时,上述代码再次起作用。因此,最终找到了问题所在。 为什么它在我的IDE配置上工作: 1.我使用JavaJDK1.7作为java_主页 2.我使用的是旧版本的Tomcat作为构建 3.为了使用JDT,您需要从.jar归档文件中提取nlmauth.dll并将其复制到配置的env JDK(jdk1.7-->bin-->copy here) 为什么它不能在Tomcat服务器上工作: 1.我使用的是Tomcat 8.xxx 2.Tomcat 8.xx需要JDk 8 3.在JDK 8中,我还没有通过nlmauth.dll(一旦我完成了这项工作,一切都正常了) 为了解决这个问题,第一条线索是查看tomcat服务器日志 根据我的推测,只有在通过集成安全性建立数据库连接时,才会发生此问题。 我的假设与以下事实有关:在tomcat日志中,拒绝JDBC驱动程序的主要错误是基于集成的安全凭据
祝大家好运。DbLogIn.java第27行-你能发布它吗?你能标记哪一行是#27吗?那将是:'PreparedStatement PreparedStatement=conn.prepareStatement(query);'你怎么知道的可能重复?“com.lifetapp.business.dataLayer.DbLogIn.isValidUser(DbLogIn.java:27)”-因此在这一行中
PreparedStatement PreparedStatement=conn.preparest陈述(查询)代码>某些内容是空的。只有两个候选人:query和conn。扰流板:不是query。。。
public class DBConnection
{
private DatabaseMetaData dma;
private static Connection con;
private static DBConnection instance = null;
private static String security = "integratedSecurity=true;";
private DBConnection()
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
} catch (Exception e) {
System.out.println("Can not find the driver");
System.out.println(e.getMessage());
}
try{
con = DriverManager.getConnection("jdbc:jtds:sqlserver://<Machine>;databaseName=<DBName>; useNTLMv2=true;");
//set autocommit
con.setAutoCommit(true);
dma = con.getMetaData(); // get meta data
System.out.println("Connection to " + dma.getURL());
System.out.println("Driver " + dma.getDriverName());
System.out.println("Database product name " + dma.getDatabaseProductName());
}
catch(Exception e){
System.out.println("Problems with the connection to the database");
System.out.println(e.getMessage());
System.out.println(con);
}
}
public static void closeConnection()
{
try{
con.close();
System.out.println("The connection is closed");
}
catch (Exception e){
System.out.println("Error trying to close the database " + e.getMessage());
}
}
public Connection getDBcon()
{
return con;
}
public static DBConnection getInstance()
{
if (instance == null)
{
instance = new DBConnection();
}
return instance;
}
public static void startTransaction()
{ try{
con.setAutoCommit(false);
}
catch(Exception e){
System.out.println("fail start transaction");
System.out.println(e.getMessage());
}
}
public static void commitTransaction()
{ try{
con.setAutoCommit(true);
}
catch(Exception e){
System.out.println("fail commit transaction");
System.out.println(e.getMessage());
}
}
public static void rollbackTransaction()
{
try
{
con.rollback();
con.setAutoCommit(true);
}
catch(Exception e){
System.out.println("fail rollback transaction");
System.out.println(e.getMessage());
}
}
public class DbLogIn implements ILogIn
{
private Connection conn;
public DbLogIn()
{
conn = DBConnection.getInstance().getDBcon();
}
public Staff isValidUser(String userName, String password)
{
Staff staff = new Staff();
try {
String query = "SELECT * FROM Staff WHERE userName=? AND pass=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setString(1, userName);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
while( resultSet.next() ) {
staff.setUserID(resultSet.getInt("userID"));
staff.setfName(resultSet.getString("fName") );
staff.setlName(resultSet.getString("lName") );
staff.setUserName(resultSet.getString("userName") );
staff.setPass(resultSet.getString("pass") );
staff.setEmail(resultSet.getString("email") );
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return staff;
}
}