jsp页面:<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="dao.TDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
TDao t=new TDao();
out.print(t.getLastDate());
%>
</body>
</html>
DBHelper.java
package tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
private static String driver="com.mysql.jdbc.Driver";
private static String username="root";
private static String password="root";
private static String url="jdbc:mysql://localhost:3306/datamanagement";
private static Connection conn=null;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
if(conn==null) {
try {
conn=DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}else {
return null;
}
}
public static void main(String[] args) {
Connection conn=DBHelper.getConnection();
if(conn==null) {
System.out.println("连接失败");
}else {
System.out.println("连接成功!!!");
}
}
}
TDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import tools.DBHelper;
public class TDao {
public String getLastDate() {
String lastDate=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try {
Connection conn=DBHelper.getConnection();
String sql="select riqi from daily_report order by riqi desc";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
rs.next();
lastDate=rs.getString("riqi");
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
rs.close();
stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return lastDate;
}
public static void main(String[] args) {
TDao t=new TDao();
System.out.println(t.getLastDate());
}
}
慕慕9558314
相关分类