### 小组:The Expendables
测试代码:
/*UsersDAO代码*/ public class UsersDAO extends MyDAO{ // private static JdbcUtil jdbcutil=new JdbcUtil(); public static boolean login(int type,String username,String password){ init(); String pwd=null; try{ switch(type){ case 1: sqlCommand="select * from 教 师 where 工号=?;"; break; case 2: sqlCommand="select * from 系负责人 where 工号=?;"; break; case 3: sqlCommand="select * from 教学办 where 工号=?;"; break; } conn=JdbcUtil.getConnection(); pst=conn.prepareStatement(sqlCommand); // pst.setString(1, tb_name); pst.setString(1, username); rs=pst.executeQuery(); while(rs.next()){ pwd=rs.getString("密码"); } }catch(Exception e){ System.out.println(e.toString()); } if(pwd==null||!pwd.equals(password)) return false; else return true; } } /*UsersDAOTest代码*/ /*测试用户登录,以教师用户为例(工号:21001,密码:21001)*/ public class UsersDAOTest { @Before public void setUp() throws Exception { } @Test public void testLogin() { boolean result=UsersDAO.login(1, "21001", "21001"); if(result) System.out.println("ok!"); else System.out.println("not ok!!"); } } |
测试结果:
DB.properties works!
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'coursemanagement.教' doesn't exist
not ok!!
检查发现错误的地方:
/*这行代码中的"教师"间多了一个空格*/
sqlCommand="select * from 教 师 where 工号=?;";
修改后的测试结果:
DB.properties works!
ok!
分析结果:
1、eclipse代码的字体需要放大,否则看不清楚中文间的空格。
2、尽量避免在代码中使用中文,但是由于建立数据库时使用了中文,所以便“将错就错”进行开发,以后会避免出现此类低级错误。(ps:其实是因为英语太渣,才不用英文命名表名和列名,担心在修改数据库时会出错)