databaseconnection.java

来自「IBM提供的购物车」· Java 代码 · 共 39 行

JAVA
39
字号
package com.webshop.db;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
/**
 * @author w
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

public class DatabaseConnection {
	public static Connection getConnection() throws SQLException,NamingException
	{
		try
		{
			
			Context initCtx = new javax.naming.InitialContext(); 
            Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
            DataSource ds = (DataSource)envCtx.lookup("jdbc/eshop"); 
  
			return ds.getConnection();
		}
		catch(SQLException e)
		{
			throw e;
		}
		catch(NamingException e)
		{
			throw e;
		}
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?