You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
3 years ago
|
package com.dao;
|
||
|
import java.sql.*;
|
||
|
import javax.sql.DataSource;
|
||
|
import javax.naming.*;
|
||
|
public interface Dao {
|
||
|
public static DataSource getDataSource(){
|
||
|
DataSource dataSource = null;
|
||
|
try {
|
||
|
Context context = new InitialContext();
|
||
|
dataSource =
|
||
|
(DataSource)context.lookup("java:comp/env/jdbc/webstoreDS");
|
||
|
System.out.println("这里到了吗");
|
||
|
}catch(NamingException ne){
|
||
|
System.out.println("异常:"+ne);
|
||
|
System.out.println("查找数据源不成功");
|
||
|
}
|
||
|
return dataSource;
|
||
|
}
|
||
|
public default Connection getConnection() throws SQLException {
|
||
|
System.out.println("这里");
|
||
|
DataSource dataSource = getDataSource();
|
||
|
System.out.println("那这");
|
||
|
Connection conn = null;
|
||
|
try{
|
||
|
System.out.println("连接");
|
||
|
conn = dataSource.getConnection(); //
|
||
|
System.out.println("连接成功");
|
||
|
}catch(SQLException sqle){
|
||
|
System.out.println("异常:"+sqle);
|
||
|
System.out.println("链接失败");
|
||
|
}
|
||
|
return conn;
|
||
|
}
|
||
|
}
|