⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userjdbcdao.java

📁 功能完善的java开发框架
💻 JAVA
字号:
package news.container;

import com.jdon.servicelocator.ServiceLocatorException;

import javax.sql.DataSource;
import com.jdon.servicelocator.ejb.ServiceLocator;

import org.apache.log4j.Logger;
import news.model.User;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

public class UserJdbcDao
    implements UserDao {
  private final static Logger logger = Logger.getLogger(UserJdbcDao.class);
  private DataSource dataSource = null;

  public UserJdbcDao(String jndiName) {
    try {
      ServiceLocator sl = new ServiceLocator();
      dataSource = (DataSource) sl.getDataSource(jndiName);
    }
    catch (ServiceLocatorException slx) {
      logger.error(slx);
    }
  }

  public User getUserByName(String username) throws Exception {
     logger.debug(" getUserByName " + username);
     Connection c = null;
     PreparedStatement ps = null;
     ResultSet rs = null;
     User ret = null;
     try {
       String GET_USER =
           "select  * from t_user where username = ?";
       c = dataSource.getConnection();
       ps = c.prepareStatement(GET_USER,
                               ResultSet.TYPE_SCROLL_INSENSITIVE,
                               ResultSet.CONCUR_READ_ONLY);
       ps.setString(1, username);
       rs = ps.executeQuery();
       if (rs.first()) {
         ret = new User();
         ret.setUserName(username);
         ret.setUserId(rs.getString("userid"));
         ret.setPassword(rs.getString("password"));

       }
     }
     catch (SQLException se) {
       throw new Exception("SQLException: " + se.getMessage());
     }
     finally {
       if (rs != null) {
         rs.close();
       }
       if (ps != null) {
         ps.close();
       }
       if (c != null) {
         c.close();
       }
     }
     return ret;
 }




}

⌨️ 快捷键说明

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