📄 userdao.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.online.dao;import com.online.pojo.User;import java.sql.*;import java.sql.Connection;import java.sql.PreparedStatement;import javax.sql.DataSource;import javax.naming.Context;import javax.naming.InitialContext;/** * * @author Administrator */public class UserDAO { private static final String GET_STMT = "SELECT * FROM EUSER WHERE username=?"; private DataSource datasource; private ResultSet rs; User user=null; public UserDAO() { try { Context ctx = new InitialContext(); datasource = (DataSource) ctx.lookup("jdbc/OnlineExamDB"); } catch (Exception e) { e.printStackTrace(); } } public User getUs(String username) { Connection con = null; PreparedStatement ps = null; try { con = datasource.getConnection(); ps = con.prepareStatement(GET_STMT); ps.setString(1, username); rs = ps.executeQuery(); while(rs.next()){ user=new User(rs.getInt("uid"),rs.getString("username"),rs.getString("password")); } } catch (SQLException se) { se.printStackTrace(); } finally { if(ps != null) { try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } if(con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } return user; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -