破百记录笔记《一》

1、class.getResourceAsStream
//读取资源文件, 设置输入流
private static InputStream is = PropertiesUtil.class.getResourceAsStream(CONFIG);

2、static 
private static String JDBC_USER;
//连接密码
private static String JDBC_PASS;

static{
try{
properties.load(is);
JDBC_DRIVER = properties.getProperty("jdbc.driver");
JDBC_URL = properties.getProperty("jdbc.url");
JDBC_USER = properties.getProperty("jdbc.user");
JDBC_PASS = properties.getProperty("jdbc.pass");
}
catch(IOException e)
{
e.printStackTrace();
}
}

3.
Field[] fields = clazz.getDeclaredFields();

//相加两个数组
private static Field[] addFields(Field[] f1, Field[] f2) {
List<Field> l = new ArrayList<Field>();
for (Field f : f1) l.add(f);
for (Field f : f2) l.add(f);
return l.toArray(new Field[f1.length + f2.length]);
}
将两个从Class.getDelaredFields获取的属性字段f1、f2,放到集合列表ArrayList中,通过集合的.toArray方法将其打印出
返回到Field[]数组,得到所有Class的所有字段。

//将rs中的值封装成一个集合
public static Collection getDatas(Collection result, ResultSet rs, Class clazz) {
try {
while (rs.next()) {
//创建类的实例
Object vo = clazz.newInstance();
//获取本对象的属性
Field[] fields = clazz.getDeclaredFields();
//获取父类的属性
Field[] superFields = clazz.getSuperclass().getDeclaredFields();
//父类的属性和自己的属性相加
Field[] allFields = addFields(superFields, fields);
//遍历所有的属性
for (Field field : allFields) {
//获得setter方法的方法名
String setterMethodName = getSetterMethodName(field.getName());
//获得setter方法
Method setterMethod = clazz.getMethod(setterMethodName, field.getType());
invokeMethod(rs, field, vo, setterMethod);
}
result.add(vo);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
throw new DataException(e.getMessage());
}
return result;
}
小结:总的作用就是将查询的ResultSet的结果存储到Collection中去。

4
stmt.executeUpdate(sql);

5
//获得主键
ResultSet rs = stmt.getGeneratedKeys();
while(rs.next()) {
//返回最后一个主键
result = rs.getInt(1);
}

6
String.valueOf()        //返回某种数据类型的字符串字样
String.valueOf(getJDBCExecutor().executeUpdate(sql));

7
select * from T_BOOK_TYPE type ORDER BY type.ID DESC

8
SELECT * FROM T_BOOK_TYPE type WHERE type.TYPE_NAME like '%" + name + "%' " +
"ORDER BY type.ID DESC";

9  
FilterDispatcher <<< is deprecated! Please use the new filters!

10
Caught Exception while registering Interceptor class org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor - interceptor -

jar:file:/C:/Users/Administrator/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/basicV

11
解决《javaEE轻量型企业运用》中输入basicValidate程序在eclipse中的项目建立,但进行了相应的修改(以适应版本与书中的不同):
(一).web.xml中filter,将<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
         改为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    注意:DOCTYPE的地址,并不是http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd 
(二).将commons-fileupload-1.2.1.jar、commons-logging-api-1.1.jar、freemarker-2.3.12.jar、ognl-2.6.11.jar、struts2-core-2.1.2.jar、xwork-2.1.1.jar
    的struts2库的使用换成:commons-fileupload-1.3.1.jar、commons-logging-api-1.1.jar、freemarker-2.3.22.jar、ognl-3.0.13.jar、
   struts2-core-2.3.28.jar、xwork-2.3.28.jar
        (三).将RegistAction-validation.xml文件中:
    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
   修改为:
    <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
    原因在于:原因是http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd已经不是dtd约束文件了,打开网址,发现opensymphony的网址已经迁移走了,
      因为xwork的东西已经并入struts2中,成为apache的一部分.所有的dtd已经移交到http://struts.apache.org/dtds/ 这个地方.

12
<field-validator type="regex">  
            <param name="regex" ><![CDATA[(\w{4,25})]]></param>  
         <message>姓名要在4-25位之间</message>  
注意:这里网上大多数人给的demo里这个param里的name参数写的是expression,我调试了好久发现   
                这样写的话这个正则表达式校验不起作用,后把param里面name参数的值改为regex就好了。  
                PS:个人猜测可能是版本的问题,如果读者用我这个例子正则表达式不起作用的话,不妨把  
                param里的name参数再改为expression试试(写法如下):  
<param name="expression" ><![CDATA[(\w{4,25})]]></param> 

13
RegistAction.java程序中必须将其中的属性进行setXXX()和getXXX()函数的设定,否则将会造成其XXXAction-validation.xml配置文件的不可用!!!
        原因:struts框架中能够自动完成表单提交属性的设置就是靠这些Action中设置好的值。

14
Component.getToolTik();

15
(1)public class MyCanvas extends Canvas{}
(2)myCanvas.repaint();
` (3)myCanvas.setPreferredSize(new Dimension(250,180));

16
File类可以:new File(String str);               熟练程度:5
JFileChooser类:new JFileChooser(new File());

17
Field
Field[] fields = clazz.getDeclaredFields();


18
String的各种方法: 熟练程度:2
String.Valueof();
String.toString();
String.substring(0, 1).toUpperCase();

19
读取资源?????????????????
private static String CONFIG = "/cfg/jdbc.properties";
private static InputStream is = PropertiesUtil.class.getResourceAsStream(CONFIG);

20
反射的应用:
        (一)
            通过一个Properties文件(含类对象),使用反射来建立一个对象值,使用newInstance()方法。
           通过一个Properties文件(含类对象和属性),使用反射来建立对象和配置设置,调用Method方法。
其中的对象已知,那么Class的获取即:      <1> Class clazz = obj.getClass();
第二种方法,若知道了类名,则获取具体类(比如Person),那么Class的获取:<2> Class clazz = Person.class;
第三种方法为: <3> Class clazz = Class.forName("Person");
(二)
   通过从反射的Class.getDeclaredField()方法来获取某个具体类的所有属性Field[],然后从SQL查询语句select执行后获得的ResultSet中获取某个属性的真实值,
   然后遍历所有属性并同样从ResultSet中获取对应真实值,调用该Class的Method来执行setter方法,从而设置参数。

21  
Properties的用法: 熟练程度:4
String getProperty(String key);   用指定的键在此属性列表中搜索属性。
Set<String> stringPropertyName(); 返回此属性列表的键集

22
Class中Field值获取后,获得setXxxx方法名的过程;
//获得setter方法的方法名
String setterMethodName = getSetterMethodName(field.getName());
//根据属性名获得setter方法的方法名
private static String getSetterMethodName(String fieldName) {
String begin = fieldName.substring(0, 1).toUpperCase();
String end = fieldName.substring(1, fieldName.length());
String methodName = "set" + begin + end;
return methodName;
}

23 
Class类的方法了解:
Class.forName();    //获取类对象的方法之一
还有两种获取类对象方法:
Class clazz = Person.class;
Class clazz = obj.getClass();  //返回运行类!!!!!!
Class类对象clazz:
clazz.newInstance();    //获取类对象
clazz.getDeclaredField();   //通过类对象获取类属性
clazz.getMethod();    //通过类对象获取类方法
其他方法:
class.getResourceAsStream(String name);
例如:PropertiesUtil.class.getResourceAsStream(CONFIG);
     作用:PropertiesUtil.class即PropertiesUtil类对象调用getResourceAsStream()方法来返回指定资源以Inputstream流形式。
     一般情况下,通过标准用法:
     FileInputStream   fis = new FileInputStream("a.txt");
     Properties pro = new Properties();
     pro.load(fis);


25
super 熟练程度:4

26
static 熟练程度:4
类中使用static块

27
java中系统属性的获取:
System.out.println("user.home = " + System.getProperty("user.home"));   C:\users\Administrator
System.out.println("os.name = " + System.getProperty("os.name")); Windows 7
System.out.println("os.arch = " + System.getProperty("os.arch")); amd64
System.out.println("os.version = " + System.getProperty("os.version")); 6.1
System.out.println("user.dir = " + System.getProperty("user.dir")); C:\users\Administrator\Desktop
System.out.println("user.name = " + System.getProperty("user.name")); Administrator

28
外部内访问内部类的方式:
class ImageViewer
{

private FolderChooser chooser;


public static void main(String[] args){
/*
ImageViewer imageview = new ImageViewer();          //<1>这种方法中未曾引入this,通过访问外部类对象的方法,也可以访问内部类中的方法
imageview.chooser.showOpenDialog(null);
*/

/*
ImageViewer imageview = new ImageViewer();
imageview.test();             //<2>虽然test为非静态方法,但是由于是imageview直接调用,就不是this.test()的形式
*/

/*
ImageViewer imageview = new ImageViewer();
ImageViewer.FolderChooser chooser = imageview.new FolderChooser(); //<3>虽然FolderChooser为非静态方法,但是由于其为imageview为前缀,所以是直接调      //用的方式,也不是this.test()的形式
chooser.showOpenDialog(null);
*/
//chooser.setControlButtonsAreShown(true);
System.out.println("user.home = " + System.getProperty("user.home"));
System.out.println("os.name = " + System.getProperty("os.name"));
System.out.println("os.arch = " + System.getProperty("os.arch"));
System.out.println("os.version = " + System.getProperty("os.version"));
System.out.println("user.dir = " + System.getProperty("user.dir"));
}

class FolderChooser extends JFileChooser{
//该类构造函数
public FolderChooser(){
//其构造函数使用继承父类的构造函数,用来给文件选择器初始目录
super(new File(System.getProperty("user.home")));
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
System.out.println("user.home = " + System.getProperty("user.home"));
}
/*
public void approveSelection(){

}
*/
}
public void test()
{
chooser = new FolderChooser();
chooser.showOpenDialog(null);
}
}

总结:1、可以通过外部类中定义方法来创建内部类并进行访问。
      2、可以直接通过外部类对象来创建内部类对象,即:FolderViewer chooser =  imageview.new FolderViewer();

29
super限定
 super和this一样,不能出现在static修饰的方法中,static修饰的方法属于类,只可能被类调用,而不是对象。
而super用于限定【该对象】调用它从父类继承的field和方法。

30
普通外部类中存在static修饰的main()方法时,不能够存在this。
      因此在外部类中的Field不能直接使用,而是应该重新定义并构造,Object obj = new Object();
      例如:知识点28中的例子修改后有:
class ImageViewer
{
public FolderChooser chooser = new FolderChooser();

public static void main(String[] args){
JFrame jf = new JFrame();
Box box = Box.createHorizontalBox();
JLabel label = new JLabel("路径:");
JTextField jt = new JTextField(30);

/*
ImageViewer imageview = new ImageViewer();           //<1>这种方法中未曾引入this,通过访问外部类对象的方法,也可以访问内部类中的方法
imageview.chooser.showOpenDialog(null);     
*/
box.add(Box.createHorizontalStrut(50));
box.add(label);
box.add(Box.createHorizontalStrut(20));
box.add(jt);
box.add(Box.createHorizontalStrut(50));

jf.add(box);
jf.pack();
jf.setLocation(400,400);
jf.setVisible(true);

ImageViewer imageview = new ImageViewer();
imageview.test();            //<2>虽然test为非静态方法,但是由于是imageview直接调用,就不是this.test()的形式

/*
ImageViewer imageview = new ImageViewer();
ImageViewer.FolderChooser chooser = imageview.new FolderChooser(); //<3>虽然FolderChooser为非静态方法,但是由于其为imageview为前缀,所以是直接调              //用的方式,也不是this.test()的形式
chooser.showOpenDialog(null);
*/

//chooser.setControlButtonsAreShown(true);
System.out.println("user.home = " + System.getProperty("user.home"));
System.out.println("os.name = " + System.getProperty("os.name"));
System.out.println("os.arch = " + System.getProperty("os.arch"));
System.out.println("os.version = " + System.getProperty("os.version"));
System.out.println("user.dir = " + System.getProperty("user.dir"));
}

class FolderChooser extends JFileChooser{
//该类构造函数
public FolderChooser(){
//其构造函数使用继承父类的构造函数,用来给文件选择器初始目录
super(new File(System.getProperty("user.home")));
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
System.out.println("user.home = " + System.getProperty("user.home"));
}
public void approveSelection(){
//调用父类覆盖的approveSelection()方法
super.approveSelection();
//设置对话框中的路径
this.setCurrentDirectory(new File(System.getProperty("user.home")));
//this.setCurrentDirectory(this.getSelectedFile());

}
}
public void test()
{
FolderChooser chooser = new FolderChooser();
chooser.showOpenDialog(null);
}
}
注意:其中main函数中的JFrame、JLabel等并不能在ImageViewer作为其的一个字段Field进行定义,而是应该
      定义并构造:
  class ImageViewer{
public static void main(String[] args)
{
private JFrame jf = new JFrame();
jf.add(xxx);     //其等同与this.jf.add();由于main中不能存在this,因此错误 
}
}
      而不是:
   class ImageViewer{
private JFrame jf = new JFrame();
public static void main(String[] args)
{
jf.add(xxx);     //其等同与this.jf.add();由于main中不能存在this,因此错误 
}
}
      那么,对于以下方式则是错误的:
   class ImageViewer{
public static void main(String[] args)
{
FolderChooser chooser = new FolderChooser();
chooser.showOpenDialog(); //由于FolderChooser是ImageViewer的内部类,等同于
} //this.FolderChooser chooser = new FolderChooser();只能改成 ImageViewer.FolderChooser chooser = new        // FolderChooser();
}                        //因此在main中要使用某个类,必须定义并构造,则为: ImageViewer imageview = new ImageViewer();
                                                       ImageViewer.FolderChooser chooser = imageview.new FolderChooser();
                                                       chooser.showOpenDialog(null);

总结:上面28和30两个部分,重点在于对内部类方法实现的三种访问方式。


class ImageViewer
{
public FolderChooser chooser = new FolderChooser();
public String str;

public static void main(String[] args){

}

public String getSelectFolder(String str){
return this.str = str;
}
class FolderChooser extends JFileChooser{
//该类构造函数
public FolderChooser(){
//其构造函数使用继承父类的构造函数,用来给文件选择器初始目录
super(new File(System.getProperty("user.home")));
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
System.out.println("user.home = " + System.getProperty("user.home"));
}


public void approveSelection(){
//调用父类覆盖的approveSelection()方法
super.approveSelection();
//设置对话框中的路径
//this.setCurrentDirectory(new File(System.getProperty("user.home")));
this.setCurrentDirectory(this.getSelectedFile());
getSelectFolder(this.getSelectedFile().getAbsolutePath());              //内部类直接调用外部类的方法!!!!!
}

}
public void test()
{
FolderChooser chooser = new FolderChooser();
chooser.showOpenDialog(null);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值