index.jsp:
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.DatabaseMetaData"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.util.Properties"%>
<%@ page import="java.io.FileInputStream"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.IOException"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Servlet example</title>
</head>
<body>
<b><%
int count;
Properties props = new Properties();
props.load(new FileInputStream(new File(application.getRealPath("WEB-INF/classes/myapp.properties"))));
String dbUrl = props.getProperty("myapp.db.url");
String dbUser = props.getProperty("myapp.db.user");
String dbPass = props.getProperty("myapp.db.password");
String[] splt = dbUrl.split(":",3);
String dbType = splt[1].toUpperCase();
String query = "select count(*) from sysuser";
Properties conProps = new Properties();
conProps.put("user", dbUser);
conProps.put("password", dbPass);
conProps.put("charSet", "Cp1251");
Connection con = DriverManager.getConnection(dbUrl, conProps);
DatabaseMetaData meta = con.getMetaData();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
count = rs.getInt(1);
out.println("Count of users = " + count);
out.println("<br>dbType=" + dbType);
}
%></b>
</body>
</html>
Комментариев нет:
Отправить комментарий