📄 userdao.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.exam.model;/** * * @author fyadmin */import java.sql.*;import javax.sql.DataSource;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;public class UserDAO { private static final String GET_EUSER = "SELECT * FROM EUSER where username=? and password=?"; public UserDAO() { } public User retrieve(String username, String password) throws SQLException { DataSource ds = null; Connection con = null; PreparedStatement ps = null;//语句 ResultSet rs = null;//结果 User users = null; try { Context ctx = new InitialContext();//得到JNDI if (ctx == null) { throw new RuntimeException("JNDI Context could not be found."); } ds = (DataSource) ctx.lookup("jdbc/examDB"); if (ds == null) { throw new RuntimeException("DataSource could not be found."); } con = ds.getConnection();//得到连接通道 ps = con.prepareStatement(GET_EUSER); ps.setString(1, username); ps.setString(2, password); rs = ps.executeQuery();//结果通道 while (rs.next()) { int objectID = rs.getInt("UID"); users = new User(objectID, rs.getString("username"), rs.getString("password")); } } catch (NamingException ne) { throw new RuntimeException("A JNDI error occured." + ne.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return users; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -