function check(){ var login_username = document.getElementById("uname"); var login_password = document.getElementById("upwd"); if(login_username.value == ""){ alert("用户名不能为空!请重新填入!"); login_username.focus(); return false; }else if(login_password.value == ""){ alert("密码不能为空!请重新填入!"); login_password.focus(); return false; } return true; } function focusOnLogin(){ var login_username = document.getElementById("uname"); login_username.focus(); }<%@include file="index-elements/index_bottom.html"%>
public class BaseDao { // 01. 基础内容的准备 private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String url="jdbc:sqlserver://localhost:1433;DataBaseName=newsS2223"; private static final String username="sa"; private static final String pwd="6375196"; //02, 接口对象的准备 Connection con=null; PreparedStatement ps=null; public ResultSet rs=null; /** * 01.写一个用户获取到一个连接对象的方法,方法的返回值是Connection类型 * @return 连接对象 * @throws Exception */ public Connection getConnection() throws Exception{ Class.forName(driver); //什么条件下,构建connection对象 if (con==null||con.isClosed()) { con=DriverManager.getConnection(url, username, pwd); } //同志们碰到一个 return con; } /** * 执行查询操作 目的:返回一个读取器 * @param sql sql语句 * @param objs 参数列表 * @return 读取器对象 * @throws Exception */ public ResultSet executeQuery(String sql,Object... objs) throws Exception{ con=getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i+1, objs[i]); } rs= ps.executeQuery(); return rs; } /** * 执行增删该操作 * @param sql sql语句 * @param objs 参数列表 * @return 受影响行数 * @throws Exception */ public int executeUpdate(String sql,Object... objs) throws Exception{ con=getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i+1, objs[i]); } int count = ps.executeUpdate(); return count; } /** * 回收连接资源 * @throws Exception */ public void closeAll() throws Exception{ //倒着回收 if(rs!=null){ rs.close(); } if (ps!=null) { ps.close(); } if(con!=null){ con.close(); } } }
public class NewsDetailDAOImpl extends BaseDao implements NewsDetailDAO{ @Test public void testAll() throws Exception{ Listlist= getAllNews() ; for (NewsDetail item : list) { System.out.println(item.getNewsTitle()); } } public List getAllNews() throws Exception { List list=new ArrayList (); String sql="select * from newsDetails"; ResultSet rs = executeQuery(sql); if(rs!=null){ while(rs.next()){ //各个列 //赋值给单个新闻对象的各个属性 NewsDetail news=new NewsDetail(); news.setNewsId(rs.getInt("newsId")); news.setNewsTitle(rs.getString("newsTitle")); news.setNewsContent(rs.getString("newsContent")); news.setNewsCreateDate(rs.getDate("newsCreateDate")); news.setNewsAuthor(rs.getString("newsAuthor")); news.setNewsCategoryId(rs.getInt("newsCategoryId")); //单个新闻对象加入新闻泛型 list.add(news); } } return list; } }
public class NewsDetail { private int newsId; private String newsTitle; private String newsContent; private Date newsCreateDate; private String newsAuthor; private int newsCategoryId; public int getNewsId() { return newsId; } public void setNewsId(int newsId) { this.newsId = newsId; } public String getNewsTitle() { return newsTitle; } public void setNewsTitle(String newsTitle) { this.newsTitle = newsTitle; } public String getNewsContent() { return newsContent; } public void setNewsContent(String newsContent) { this.newsContent = newsContent; } public Date getNewsCreateDate() { return newsCreateDate; } public void setNewsCreateDate(Date newsCreateDate) { this.newsCreateDate = newsCreateDate; } public String getNewsAuthor() { return newsAuthor; } public void setNewsAuthor(String newsAuthor) { this.newsAuthor = newsAuthor; } public int getNewsCategoryId() { return newsCategoryId; } public void setNewsCategoryId(int newsCategoryId) { this.newsCategoryId = newsCategoryId; } }