不通过ajax进行数据库的读取
一种将代码写在jsp中,但是把数据库、用户名、密码都放在jsp中,安全系数太大了,这里随便记一下
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*,java.sql.SQLException" %>
<%
//把经常需要修改的数据放在最上面,以方便修改
String username="root";//数据库用户名
String password="123456";//数据库密码
//jsp程序中半数以上的Exception是NonePointerException,在声明变量时赋予一个初始值,能缩短调试时间
ResultSet baicheng=null;//数据库查询结果集
ResultSet totle=null;
ResultSet newreplace=null;//数据库查询结果集
ResultSet newdirect=null;//数据库查询结果集
ResultSet online=null;//数据库查询结果集
ResultSet notice=null;//数据库查询结果集
Connection conn=null;
Statement stmt=null;
//注册驱动程序
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(java.lang.ClassNotFoundException e)
{
System.err.println("Driver Error"+e.getMessage());
}
//连接数据库并创建Statement对象
String url="jdbc:mysql://localhost:3306/qingneng_test?useUnicode=true&characterEncoding=utf-8";
try
{
conn=DriverManager.getConnection(url,username,password);
stmt=(Statement)conn.createStatement();
}catch(Exception e)
{
System.err.println("数据库连接错误:"+e.getMessage());
}
//通过Statement执行SQL语句来获取查询结果
try
{
totle=(ResultSet)stmt.executeQuery("select * from totle");
}catch(SQLException ex)
{
System.err.println("数据库查错误:"+ex);
}
%>
<%
int i=0;
//利用while循环输出各条记录
while(totle.next())
{i=i+1;
%>
<%=totle.getString("mei") %>
<%
}
%>
以上就是在jsp中使用java代码
第二种就是将读取数据库的操作都写在controller中,通过return传到jsp中
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.qingneng.entity.ProjectEnum;
import com.qingneng.service.DemandService;
import java.sql.*;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
/**
* Created by chenyj on 15/11/26.
*/
@Controller
@RequestMapping(value = "/")
public class IndexController extends BaseController{
@Autowired
DemandService demandService;
@RequestMapping(value = "index",method = RequestMethod.GET)
public ModelAndView getIndex(HttpServletResponse response, HttpServletRequest request){
//System.out.println("test12345");
BigDecimal newReplace = demandService.getTotal(ProjectEnum.EnergyType.NEW.getValue(),ProjectEnum.RequirementType.REPLACED_POWER.getValue());
BigDecimal newDirect = demandService.getTotal(ProjectEnum.EnergyType.NEW.getValue(),ProjectEnum.RequirementType.BUY_ENTERPRISE.getValue());
BigDecimal oldReplace = demandService.getTotal(ProjectEnum.EnergyType.OLD.getValue(),ProjectEnum.RequirementType.REPLACED_POWER.getValue());
BigDecimal oldDirect = demandService.getTotal(ProjectEnum.EnergyType.OLD.getValue(),ProjectEnum.RequirementType.BUY_ENTERPRISE.getValue());
ModelAndView modelAndView = new ModelAndView("home/index"); //定义路径,JSP路径。真实地址为WEB-INF/jsp/home/index.jsp
String username="root";//数据库用户名
String password="123456";//数据库密码
//jsp程序中半数以上的Exception是NonePointerException,在声明变量时赋予一个初始值,能缩短调试时间
ResultSet coal=null;
ResultSet role=null;
ResultSet role1=null;
ResultSet requirement=null;
ResultSet enquiry=null;
ResultSet transaction=null;
Connection conn=null;
Statement stmt=null;
//注册驱动程序
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(java.lang.ClassNotFoundException e)
{
System.err.println("Driver Error"+e.getMessage());
}
//连接数据库并创建Statement对象
String url="jdbc:mysql://localhost:3306/qingneng_test?useUnicode=true&characterEncoding=utf-8";
try
{
conn=DriverManager.getConnection(url,username,password);
stmt=(Statement)conn.createStatement();
}catch(Exception e)
{
System.err.println("数据库连接错误:"+e.getMessage());
}
//通过Statement执行SQL语句来获取查询结果
try
{
coal=(ResultSet)stmt.executeQuery("select * from coal_consumption;");
}catch(SQLException ex)
{
System.err.println("数据库查错误:"+ex);
}
double savecoal=0;
//利用while循环输出各条记录
try{
while(coal.next())
{savecoal=savecoal+coal.getDouble("save_amount");
}
savecoal=savecoal/10000000;
coal.close();
}catch(Exception e){
System.out.println("取值错误!");
}<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Monaco; color: rgb(126, 80, 79);">modelAndView<span style="color: #000000">.addObject(</span><span style="color: #3933ff">"savecoal"</span><span style="color: #000000">, </span>savecoal<span style="color: #000000">);</span><span style="color: #4e9072">//测试,传字符串</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Monaco; color: rgb(126, 80, 79);"><span style="color: #931a68">return</span><span style="color: #000000"> </span>modelAndView<span style="color: #000000">;}}</span></p>
截取了我写的部分代码,最后几个引号的个数可能不对,但是大概意思是这样的。
在jsp中按照${savecoal}这样来引用。