1. 程式人生 > >jsp連結sqlserver2008資料庫

jsp連結sqlserver2008資料庫

  最近在學習java,連資料庫的時候很鬱悶,main方法連結正確,jsp一呼叫就錯誤,後臺發現sqljdbc4.jar這個jar 在main中和在jsp中 是不一樣的。

在main中只需右鍵專案build path 新增進sqljdbc4.jar 就好了,而在jsp中 你需要把sqljdbc4.jar copy到WebRoot/Web-inf/lib中 

新增完以後,系統會自動幫你處理,2邊都可以使用。大笑 

1:DBHelper類:

package news2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dbHelper {
	private static final String DRIVER_CLASS ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;databasename=news";
	
	private static final String DATABASE_USER ="sa";
	private static final String DATABASE_PASSWORD = "123456";
	
	public static Connection getConnection(){
		Connection dbConnection = null;
		try{
			Class.forName(DRIVER_CLASS);
			dbConnection = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return dbConnection;
		
	}
	
	public static void closeConnection(Connection dbConnection){
		try{
			if(dbConnection != null && (!dbConnection.isClosed())){
				dbConnection.close();
			}
		}
		catch(SQLException sqlE){
			sqlE.printStackTrace();
		}
	}
	
	public static void closeResultSet(ResultSet res){
		try{
			if(res != null){
				res.close();
				res = null;
			}
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public static void closeStatement(Statement stat){
		try{
		      if(stat != null){
			     stat.close();
				 stat = null;
			  }
		   }
		   catch(SQLException e){
			  e.printStackTrace();
		   }
}
}


2:呼叫DBHelper類

package news2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Control {

	public List<newsModel> getAllFirstLevelTitleList2(){
		List<newsModel> list =new ArrayList<newsModel>();
		Connection dbConnection = null;
		PreparedStatement pStatement = null;
		ResultSet res = null;
		try{
			dbConnection = dbHelper.getConnection();
			String sql = " select * from FirstLevelTitle order by Createtime desc ";
			pStatement  = dbConnection.prepareStatement(sql);
			res = pStatement.executeQuery();
			while(res.next()){
				int id = res.getInt("id");
				String Title = res.getString("TitleName");
				String creator = res.getString("Creator");
				Date time = res.getDate("CreateTime");
				newsModel fTitle = new newsModel(id, Title, creator, time);
				list.add(fTitle);
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			dbHelper.closeResultSet(res);
			dbHelper.closeStatement(pStatement);
			dbHelper.closeConnection(dbConnection);
		}
		return list;
	}
}

3:Model類

package news2;

import java.util.Date;

public class newsModel {
	private int id;
	private String titleName;
	private String creator;  //建立者
	private Date createTime;
	
	public newsModel(int id,String titleName,String creator,Date createTime)
	{
		this.id = id;
		this.titleName = titleName;
		this.creator = creator;
		this.createTime = createTime;
	}
	
	
	public int getId(){
		return this.id;
	}
	
	public String getTitleName(){
		return titleName;
	}
	
	
	public String getCreator(){
		return creator;
	}
	

	public Date getCreateTime(){
		return createTime;
	}
	
	
	
	public void setId(int par){
		this.id = par;
	}
	
	
	
	public void setTitleName(String par){
		this.titleName = par;
	}
	
	
	
	public void setCreator(String par){
		this.creator = par;
	}
	
	
	
	public void setCreateTime(Date par){
		this.createTime = par;
	}
}



3:直接測試看結果,在mian函式中
package news2;

import java.util.*;

public class demo {
	public static void main(String[] args){
		Control c = new Control();
		List<newsModel> list = c.getAllFirstLevelTitleList2();
		System.out.println(list.size());
	}
}


4:直接測試看結果,在 jsp頁面中

<%@page import="news2.newsModel"%>
<%@page import="news2.Control"%>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <% Control c = new Control();
    	List<newsModel> list = c.getAllFirstLevelTitleList2();
     %>
     <%=list.size() %>
  </body>
</html>