parent
fd8108b2f9
commit
b2bc9ff63b
@ -0,0 +1,34 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue