您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“在JSP中三種連接字符串的配置方式”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
很多初學(xué)者會(huì)不知道怎么配置連接字符串而煩惱,今天筆者就寫一些很實(shí)用的三種配置連接字符串的方式,當(dāng)然簡單的那種我沒有寫,那種在公司的開發(fā)中也不實(shí)用,總結(jié)不好請(qǐng)指教。
一、連接池方式:
1、連接遲3個(gè)包+sqlserver驅(qū)動(dòng)包復(fù)制到tomcat\common\lib
2、配置tomcat\conf\context.xml,注意2000和2005 驅(qū)動(dòng)名字和路徑
<Resource name="jdbc/pubs" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="120010" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/> 2000: driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/> 2005: driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=books"/>
3、在工程web.xml添加節(jié)點(diǎn)
<resource-ref> <res-ref-name>jdbc/pubs</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
4、得到連接的方法內(nèi)導(dǎo)如以下幾個(gè)包:
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; //得到Connection對(duì)象的方法 public static Connection getConnection(){ try { Context ic = new InitialContext(); DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/bbs"); con = source.getConnection(); }catch(NamingException ex){ ex.printStackTrace(); }catch(SQLException ex){ ex.printStackTrace(); } return con; }
二、讀取屬性文件方式
*.properties文件 driverName=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost:1433;DatabaseName=books user=sa password=123 //讀取*.properties文件的類 import java.io.InputStream; import java.util.Properties; public final class Env extends Properties { private static Env instance; public static Env getInstance(){ if(instance != null){ return instance; }else{ makeInstance(); return instance; } } //synchronized 同步方法,保證同一時(shí)間只能被一個(gè)用戶調(diào)用 private static synchronized void makeInstance(){ if(instance == null){ instance = new Env(); } } private Env(){ InputStream is =getClass().getResourceAsStream("db.properties");//配置文件位置 try{ load(is); }catch(Exception ex){ System.err.println("請(qǐng)確認(rèn)讀取的文件是否存在!"); } } public static void main(String[] args) { System.out.println(getInstance().getProperty("driverName")); } }
注意類的調(diào)用:譬如String url = Env.getInstance().getProperties("url");就能得到相應(yīng)的字符串
驅(qū)動(dòng)driverName,用戶user, 密碼password獲取方式同上
三、讀取xml文件中的節(jié)點(diǎn)方式
首先報(bào)連接池包3個(gè)和1個(gè)數(shù)據(jù)庫驅(qū)動(dòng)包復(fù)制到工程下WEB-INF\lib中
1、工程下的web.xml要添加以下節(jié)點(diǎn):
<context-param> <param-name>driverName</param-name> <param-value>com.microsoft.sqlserver.jdbc.SQLServerDriver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>jdbc:sqlserver://localhost:1433;DatabaseName=bbs</param-value> </context-param> <context-param> <param-name>userName</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>passWord</param-name> <param-value>123</param-value> </context-param>
2、新建一個(gè)普通類繼承HttpServlet類,并實(shí)現(xiàn)ServletContextListener監(jiān)聽接口,如下:
import java.sql.Connection; import java.sql.ResultSet; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpServlet; public class ContextListener extends HttpServlet implements ServletContextListener { /** * 銷毀servlet */ public void contextDestroyed(ServletContextEvent sc) { } /** * 初始化 */ public void contextInitialized(ServletContextEvent sc) { System.out.println("開啟:"); ServletContext servletContext = sc.getServletContext(); String driverName = servletContext.getInitParameter("driverName"); String url = servletContext.getInitParameter("url"); String userName = servletContext.getInitParameter("userName"); String passWord = servletContext.getInitParameter("passWord"); BaseDAO.setDriverName(driverName); BaseDAO.setUrl(url); BaseDAO.setUser(userName); BaseDAO.setPassword(passWord); } }
3、在公共類BaseDao中
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; //連接池要到包 /* * 獲取數(shù)據(jù)庫連接 */ public class BaseDAO { private static Connection con; private static String driverName; private static String url; private static String userName; private static String passWord; //獲取連接對(duì)象Connection public static Connection getConnection(){ BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverName); dataSource.setUrl(url); dataSource.setUsername(userName); dataSource.setPassword(passWord); try{ con = dataSource.getConnection(); } catch(SQLException ex) { ex.printStackTrace(); } return con; } /* * 配置:從web.xml */ //驅(qū)動(dòng)名稱 public static String getDriverName() { return getDriverName(); } public static void setDriverName(String driverName) { BaseDAO.driverName = driverName; } //URL public static String getUrl(){ return getUrl(); } public static void setUrl(String url) { BaseDAO.url = url; } //用戶名 public static String getUser(){ return getUser(); } public static void setUser(String userName) { BaseDAO.userName = userName; } //密碼 public static String getPassWord(){ return getPassWord(); } public static void setPassword(String passWord) { BaseDAO.passWord = passWord; } }
“在JSP中三種連接字符串的配置方式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。