我正在尝试使用Microsoft SQL Server的jdbc驱动程序连接到我的数据库,但我设法与服务器建立了连接,但是发生了一些奇怪的事情。这是我的Dconnector类:
package com.myblog;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.sql.*;
import java.util.Properties;
public class Dconnector {
public Connection connection = null;
public Statement stmt;
static Properties prop = null;
public Dconnector() throws SQLException {
DriverManager.registerDriver(new SQLServerDriver());
System.out.println("SQL JDBC Connection Testing");
prop = new Properties();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String DB_CONN_STRING = "jdbc:sqlserver://localhost:1434;databaseName=blogdb;";
//Provided by your driver documentation. In this case, a MySql driver is used :
String USER_NAME = "sa";
String PASSWORD = "pwd";
connection =DriverManager.getConnection(DB_CONN_STRING, USER_NAME, PASSWORD);
stmt = connection.createStatement();
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("Connected!");
} else {
System.out.println("Failed to connect!");
}
}
public ResultSet queryResult(String Query) {
ResultSet rst = null;
try {
rst = stmt.executeQuery(Query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rst;
}
public int queryCount(String Query) {
int count = 0;
try {
count = stmt.executeUpdate(Query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public void closeConnection()
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
奇怪的是,当我启动Web应用并像下面这样创建Dconnector的新实例时:
com.myblog.Dconnector db=new com.myblog.Dconnector();
并使用以下命令将其关闭:
db.closeConnection();
由于某种原因,该班级似乎被低估了3次?在控制台中,依次单击“ SQL JDBC连接测试”和“已连接!”。显示3次。
此外,当我尝试在连接打开与关闭之间的任何查询中调用queryResult时,所有内容都将中断,并且读取预登录响应时出错,并且连接重置再次重复3次。
这是.jsp文件(脚本)中使用的代码:
com.myblog.Dconnector db=new com.myblog.Dconnector();
ResultSet rst=db.queryResult("SELECT Caption FROM ArticlesTB");
ResultSetMetaData rsmd = rst.getMetaData();
int columnsNumber = rsmd.getColumnCount();
try {
while (rst.next()) {
for (int i = 1; i <= columnsNumber; i++) {
String columnValue = rst.getString(i);
System.out.print("<h1>" +columnValue + "
</h1>"+System.getProperty("line.separator"));
}
}}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("This did not work");
}
db.closeConnection();
我现在只是输出到控制台进行测试。控制台输出的相关部分如下:
Connected to server
[2018-09-05 11:53:39,507] Artifact Stranka:war exploded: Artifact is being
deployed, please wait...
05-Sep-2018 23:53:40.372 INFO [RMI TCP Connection(3)-127.0.0.1]
org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned
for TLDs yet contained no TLDs. Enable debug logging for this logger for a
complete list of JARs that were scanned but no TLDs were found in them.
Skipping unneeded JARs during scanning can improve startup time and JSP
compilation time.
[2018-09-05 11:53:40,435] Artifact Stranka:war exploded: Artifact is deployed
successfully
[2018-09-05 11:53:40,435] Artifact Stranka:war exploded: Deploy took 928
milliseconds
-------- SQL JDBC Connection Testing ------------
You made it, take control your database now!
(h1)This is a sample article(/h1)
(h1)This is a second article.(/h1)
(h1)This is a third sample.(/h1)
-------- SQL JDBC Connection Testing ------------
You made it, take control your database now!
(h1)This is a sample article(/h1)
(h1)This is a second article.(/h1)
(h1)This is a third sample.(/h1)
-------- SQL JDBC Connection Testing ------------
You made it, take control your database now!
(h1)This is a sample article(/h1)
(h1)This is a second article.(/h1)
(h1)This is a third sample.(/h1)
05-Sep-2018 23:53:49.219 INFO
[ContainerBackgroundProcessor[StandardEngine[Catalina]]]
org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
application directory [C:\apache-tomcat-9.0.11\webapps\manager]
05-Sep-2018 23:53:49.312 INFO
[ContainerBackgroundProcessor[StandardEngine[Catalina]]]
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web
application directory [C:\apache-tomcat-9.0.11\webapps\manager] has finished
in [93] ms
已编辑HTML标记以保持代码格式。