13-05-16总结
今天主要看了swing入门级课程前三讲,并跟着视频敲写了所有代码。
 
总结如下:
1.由于以前没做过项目,不知道开发流程。今天学到了MVC初级思想。先建立model,view,util,包(package),再建立对应的类(class)。
2.通过查找资料,理解了dao包的具体含义。----DAO层一般有接口和该接口的实现类! 接口用于规范实现类! 实现类一般用于用于操作数据库! 一般操作修改,添加,删除数据库操作的步骤很相似,就写了一个公共类DAO类 ,修改,添加,删除数据库操作时 直接调用公共类DAO类!
3.JDBC数据库连接
1 package tk.dongyeblog.util; 2 3 import java.sql.Connection; 4 5 import java.sql.DriverManager; 6 7 8 public class DbUtil { 9 10 private String dbUrl = "jdbc:mysql://localhost:3306/db_book"; 11 12 private String dbUser = "root"; 13 14 private String dbPassword = "root"; 15 16 private String jdbcName = "com.mysql.jdbc.Driver"; 17 18 19 /** 20 21 * 获取数据库连接 22 23 * 24 25 * @return 26 27 * @throws Exception 28 29 */ 30 31 public Connection getCon() throws Exception { 32 33 Class.forName(jdbcName); //加载数据库驱动 34 35 Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword); 36 37 return con; 38 39 } 40 41 //关闭数据库连接 42 43 public void closeCon(Connection con) throws Exception { 44 45 if (con != null) { 46 47 con.close(); 48 49 } 50 51 } 52 53 public static void main(String[] args) { 54 55 DbUtil dbUtil = new DbUtil(); 56 57 try { 58 59 dbUtil.getCon(); 60 61 System.out.println("数据库连接成功"); 62 63 } catch (Exception e) { 64 65 // TODO Auto-generated catch block 66 67 e.printStackTrace(); 68 69 } 70 71 } 72 73 }
4.UserDao.class
1 public User login(Connection con, User user) throws Exception { 2 3 /** 4 5 * 登录验证 6 7 */ 8 9 User resultUser = null; 10 11 String sql = "select * from t_user where userName=? and password=?"; 12 13 PreparedStatement pstmt = con.prepareStatement(sql); 14 pstmt.setString(1, user.getUserName()); 15 pstmt.setString(2, user.getPassword()); 16 ResultSet rs = pstmt.executeQuery(); 17 18 19 if (rs.next()) { 20 21 resultUser = new User(); 22 23 resultUser.setUserName(rs.getString("userName")); 24 25 resultUser.setPassword(rs.getString("password")); 26 27 } 28 29 return resultUser; 30 31 } 32 33 }
5.PrepareStatement与Statement的区别
a.Statement用于处理静态 SQL 语句, PreparedStatement用于处理动态SQL语句
b.创建时的区别:
1 Statement stm=con.createStatement(); 2 3 PreparedStatement pstm=con.prepareStatement(sql);
执行的时候:
 stm.execute(sql); 
 pstm.execute(); 
c.pstm一旦绑定了SQL,此pstm就不能执行其他的Sql,即只能执行一条SQL命令。
stm可以执行多条SQL命令。
d.对于执行同构的sql(只有值不同,其他结构都相同),用pstm的执行效率比较的高,对于异构的SQL语句,Statement的执行效率要高。 当需要外部变量的时候,pstm的执行效率更高.
总的来说,实际开发中只用到prepareStatement,可以不使用Statement。
6.swing中有用代码记简单美化
// 设置最大化
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
简单美化可以用添加小图片来实现。
师父的神奇代码:设置默认字体格式和字体大小
1 Font font = new Font("Dialog", Font.PLAIN, 12); 2 3 java.util.Enumeration keys = UIManager.getDefaults().keys(); 4 5 while (keys.hasMoreElements()) { 6 7 Object key = keys.nextElement(); 8 9 Object value = UIManager.get(key); 10 11 if (value instanceof javax.swing.plaf.FontUIResource) { 12 13 UIManager.put(key, font); 14 15 } 16 }
7.相同的功能方法可以封装一个类去实现。如StringUtil类判断值是否为空
1 package tk.dongyeblog.util; 2 3 public class StringUtil { 4 5 public static boolean isEmpty(String str){ 6 7 if("".equals(str) || str==null){ 8 9 return true; 10 11 } 12 13 return false; 14 15 } 16 17 }

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号