0
点赞
收藏
分享

微信扫一扫

使用IDEA学习JSP代码第028课


new397.properties

jdbc.driver_class = com.mysql.jdbc.Driver
jdbc.connection.url = jdbc:mysql://localhost:3306/test
jdbc.connection.username = root
jdbc.connection.password = mysql123

new398.java

package pack03;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class new398_ConfigManager
{
private static new398_ConfigManager configManager;

private static Properties properties;

public new398_ConfigManager()
{
String configFile = "new397.properties";
//使用idea,在web目录下新建一个文件夹,然后标记为resources,将properties文件放入其中。
properties = new Properties();
InputStream in = new398_ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
try
{
properties.load(in);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}

}

public static new398_ConfigManager getInstance()
{
if(configManager == null)
{
configManager = new new398_ConfigManager();
}

return configManager;
}

public String getString(String key)
{
return properties.getProperty(key);
}
}

new399.jsp

<%@ page import="java.sql.*" %>
<%@ page import="pack03.new398_ConfigManager" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="../other/boot/css/bootstrap.min.css">
<script type="text/javascript" src="../other/jQuery/jQuery-3.4.1.js"></script>
<script type="text/javascript" src="../other/boot/js/bootstrap.min.js"></script>



</head>
<body>
<%!

public void getNewsList()
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

String driver = new398_ConfigManager.getInstance().getString("jdbc.driver_class");
String url = new398_ConfigManager.getInstance().getString("jdbc.connection.url");
String username = new398_ConfigManager.getInstance().getString("jdbc.connection.username");
String password = new398_ConfigManager.getInstance().getString("jdbc.connection.password");

try
{
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("select a,b,c from table1"); //运行后,提示错误,表不存在
while (rs.next())
{
int id = rs.getInt("a");
String title = rs.getString("b");
String summary = rs.getString("c");
String content = rs.getString("content");
String author = rs.getString("author");
Timestamp time = rs.getTimestamp("createdate");
System.out.println(id + "\t" + title + "\t" + summary + "\t" + content + "\t" + author + "\t" + time);
//以上均为模拟代码,供参考
}

}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs != null)
{
rs.close();
}

if(stmt != null)
{
stmt.close();
}

if(conn != null)
{
conn.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}


}
%>

<%
getNewsList(); //表没有建立,提示错误
%>
</body>
</html>

举报

相关推荐

0 条评论