??xml version="1.0" encoding="utf-8" standalone="yes"?>
下面|列了各U数据库使用JDBCq接的方式,可以作ؓ一个手册用?
1、Oracle8/8i/9i数据库(thin模式Q?
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
String url="jdbc:db2://localhost:5000/sample"; //sampleZ的数据库?nbsp;
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDBZ的数据库?nbsp;
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB为数据库?nbsp;
Connection conn= DriverManager.getConnection(url);
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库?nbsp;
Connection conn= DriverManager.getConnection(url);
String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库?nbsp;
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;
]]>
1.默认的Q点类型是双精度(doubleQ,要想要一个float必须在QҎ后面加F或者f。如Qfloat pi = 3.14;是错误的
2.点型{化ؓ整型Ӟ不进行四舍五入,直接截断数点后面的?Java的类型检查很严格Q从低精度{换到高精度是无须昑ּ转换的,double d = 123;。但是反q来Q进行窄化{换,由高_ֺ向低_ֺQ或者一U类型到另一U类型,则必M用强制类型{化?br />
3.在数l声明中包含数组长度永远是不合法的!如:int[5] arr; 。因为,声明的时候ƈ没有实例化Q何对象,只有在实例化数组对象ӞJVM才分配空_q时才与长度有关?br />
4.对象cd的数l虽然被默认初始化了Q但是ƈ没有调用其构造函数。也是_Car[] myCar = new Car[10];只创Z一个myCar数组对象Qƈ没有创徏Car对象的Q何实例!q与单纯的new一个对象不同!单个的new对象Ӟ׃调用相应的构造函数?br />
5.数组有length属性,字符串有lengthQ)Ҏ
6.String中的每个字符都是一?6位的Unicode字符Q用Unicode很容易表达丰富的国际化字W集Q比如很好的中文支持。char中可以保存一个中文汉字,应ؓchar也是一?6位的Unicode字符度ؓ0?br />
8.String对象是不变的Q可以变化的是String对象的引用?br />
9.String的不变性的机制昄会在String帔R内有大量的冗余。如Q?1" + "2" + "3" +......+ "n" 产生了n+(n+1)个String对象Q因此JavaZ更有效地使用内存QJVM留出一块特D的内存区域Q被UCؓ“String帔R?#8221;。对 String多么照顾啊!当编译器遇见String帔R的时候,它检查该池内是否已经存在相同的String帔R。如果找刎ͼ把新常量的引用指向现有?StringQ不创徏M新的String帔R对象?br />
10.java中的对象是在堆中被创建的QnewQ,但常量类的String有点特别Q像“haha”q样的字W串ҎW?条是存放在String帔R池的Q而String s=new String("haha");实在堆中被创徏的?br />
11.throw和throws关键字。throw用在Ҏ体中抛出异常Q后面是一个具体的异常对象。throws用在Ҏ参数列表括号的后面,用来声明此方法会抛出的异常种c,后面跟着一个异常类?br />
12.要把相应的System.out和System.err信息输入到文件中。代码:
System.out.println("没上");
System.setOut(new PrintStream(new FileOutputStream(new File( "c:/test.txt "))));
System.out.println( "haha ");
System.out.println("error");
} catch (Exception e) {
e.printStackTrace();
}
]]>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
//如有多个spring配置文g用逗号隔开
</context-param>
<!-- 通过Listener配置Spring容器随Web应用的启动而初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
二、struts1的启动方?br />
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml" />
</plug-in>
]]>
Struts只允怸个应用中配置一?/span> ActionServletQ但是可以将ActionServlet扩展?/span>
Servlet容器在启动时Q或者用户首ơ请?/span>ActionServlet时加?/span>ActionServletc,在上q情况下Q?/span>Servlet容器都会?/span>ActionServlet被加载后立即执行它的init()Ҏ?/span>Init()q程如下Q?/span>
1.调用initInternal()ҎQ初始化struts框架内在的消息资源,如与pȝ日志相关的通知、警告和错误信息?/span>
2.调用initOther()Q从web.xml文g中加?/span>ActionServlet的初始化参数Q如config
参数
3.调用initServlet(),?/span>web.xml文g中加?/span>ActionServlet?/span>URL映射信息。此外还会注?/span>web.xml?/span>
struts配置文g所使用?/span>DTD文gQ这?/span>DTD文g用来验证web.xml?/span>struts配置文g的语?/span>
4.调用initModuleConfig()Q加载ƈ解析默认子应用模块的struts配置文gQ创?/span>ModuleConfig对象Q把它存?/span>ServletContext?/span>
5.调用intiModuleMessageResources()Q加载ƈ初始化默认子应用模块的消息资源,创徏
MessageResources对象Q把它存?/span>ServletContext?/span>
6.调用initModuleDataSources()ҎQ加载ƈ初始化默认子应用模块的数据源。如果没有在struts配置文g中没有定?/span><data-sources>元素Q就忽略q一程
7.调用initModulePlugins(),加蝲q初始化默认子应用模块的所有插?/span>
8.当默认子应用模块被成功初始化后,如果应用q包括其他子应用模块Q将重复程4-7Q分别对其他子应用模块进行初始化。扩?/span>ActionServletc,定义如下的类Q?/span>
<?/span>>
The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.)
2.如果有包含关p?composition),那么它的成员对象按照声明的顺序被构?
Member initializers are called in the order of declaration.
3.zcL造函数的内容(body)被执?
The body of the derived-class constructor is called.
一个实?
输出:
Meal()
Lunch()
Cake()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
main()函数中要构造一个Sandwich的对?调用(q不是执?它基cPortableLunch的构造函?PortableLunch又? 归的调用,然后是Meal,Meal是承的最底层的基c?不算Object)所以它的构造函数首先被执行,然后按次序返回到Lunch, PortableLunch,但在PortableLunch的构造函数被执行之前,它的成员对象Cake a先按照声明的序被构?然后执行PortableLunch(),接着是Sandwich的成员对?最后是Sandwich().
?被注释掉的代?base-class的对象作为derive-class的成员对?q样会递归无法l束,最后程序因堆栈耗尽而结?Exception in thread main java.lang.StackOverflowError).
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1134115(?
This Tutorial will explain you, what struts is, how it builds itself up and why it is advantage to use it for your web application. First, I will provide you some informations about Java Servlets, Java Server Pages and Java Beans, because they are parts of struts.
Servlets represents java programs that runs on a web server. They allow the developer to produce dynamic web sites with java.
A Servlet has the following tasks
It reads and processed data, which a user typed in a HTML form on a web page.
If necessary other informations will be processed. For Example what browser or system will be used.
It generate results with the existing data. It calls the business logic directly in the servlet or another class, which contain the logic or executes a database query.
The results will be formated. If the browser. If the Browser expects an answer in the HTML format, then the results must be formatted in accordance with the standard. It is possible to return different formats of data with a servlet. (gif, jpeg, doc, etc.).
Suitable answer parameters are set. Befor the servlet return the data to the browser, it sends some parameter. The parameter contains the format, that will returned by the servlet, what time the browser use to cache the site and some more.
Return the document, in the format that it sends befor, to the browser.
JavaServer Pages (JSP) are text documents, which are similar to HTML Files. But you find also java code in the JSP File. JavaServer Pages allow you to mix regulare, static HTML with dynamic generated contents of servlets. The java code is inserted in the HTML document on a JSP File, differently to a servlet, where the HTML code is embedded in the java code.
Java BeansJava Beans are nothing else as classes, which keep a fixed naming convention, defined by Sun, for their event processing and methods. The attributes (variables) of the java beans are private. The access to these attributes is managed by access methods. The java specification specify these access methodes (getter and setter methods). If a java bean contains a attribute name, you can set or get the value of the attributes with the methode setName() or getName().
The business logic is the core of the application. The processes are implemented in the business logic to manage the data. For Example: when someone borrows a book in a library, that is a process in the business logic. The process change the state of the data (is the book borrowsed or not) or reads the state and then provide this information for a dialog.
This short outline should be sufficient, in order to understand the structure of struts.
Why is struts helpful?Some peoples develop web applications with Perl or PHP and implement their SQL Querys and the business logics directly in the HTML document.
The source code looks like the following example:
You can develop in this way, when you use java servlets or JSP. It is convenient in small projects. But imagine you have 70 dialogs, many database querys in this dialogs and you want to define a field status, setting if a book is deleted or not.
Good luck
In order to alter functions and database querys easily, we should seperate these from the dialogs.
Advantage of separationChanges on the functionality, without working in the dialogs.
Better overview, functionality not mixed with the dialogs.
Easy maintain a application
Different dialogs, but the same functionality
In order to control the interaction between the dialogs and business processes, you need a central control unit. This control unit manage all importants courses of the application, when which business process and which dialog will be used.
You have some disadvantages, if you implement the order of the processes directly to the business logic.
You can not reuse a part of the processes. In the following picture the process "Give Money" will bring you to the process "Select sort of icecream" every time.
If you want to change the order of the processes or add more options, you have to change it directly in the business logic.
It is easy to change the order of the business processes.
Better overview, which dialogs will be used on which business processes.
A central place to control the interaction between business processes and dialogs.
The name of this model is Model-View-Controller (MVC)
Model (business logic / business processes ? Java Beans)
View (dialogs ? JavaServer Pages)
Controller (central control unit - Java Servlets)
You will find more about the MVC later in this tutorial.
If you do not want to translate a dialog, you put the dialog texts directly into the JSP File.
Struts call the locale specific resource file by using the browser settings. You may overwrite this setting. It is quite easy to build an internationalized application with struts.
Struts saves error messages in the business logic and can display this in the dialogs. You can show all errors or bind an error to a field (ex. Emailaddress incorrect) and show the error beside of it.
The error messages will be added to the resource files, so the errors can be internationalised too.
Whether an input field of a form contains a valid date, an email, a number or something else, in each application you have to check it. Struts supports a complete solution to validate form fields. You only have to define which form field will be checked and which error message will be displayed.
Components of strutsJavaServer Pages (JSP) take over the role of dialogs in struts,
Java Beans take over the business logic and business processes and
Java Servlets take over the central control unit
Thats why we talk about three great parts of struts.
Model (business logic / business processes ? Java Beans)
View (dialogs ? JavaServer Pages)
Controller (central control unit - Java Servlets)
The model represent the actual state of the application. Two kinds of java beans are used. There are java beans which contains the data of a form or data to display (ex. The books of a library) and java beans which includes the functionality of the application or call the business logic (when a user borrows a book).
The view component is responsible for the presentation of the data. The java server pages contain HTML, XML and Java Script, like a normal HTML site. Futhermore you can use java code. Struts provide tag libraries, a summary of functions, which can be use to prepare the data for displaying.
The last component is the controller. The controller manage the request of the web browser, which a user called by an address (URL). But also forward to an action which are execute and which dialogs will be used to display the informations.
The picture below illustrates the interaction between these components.
If the user sends a query with a browser, the controller(servlet) gets and processes this query. It decides which action will be called or to which view component it must be forward.
After the controller calls an action, the action can read data from a database and provide this data to the model component, java beans. The action (business logic) returns the "next step" to the controller. The controller checks what kind is the next step. (JSP View, next action, ...) and forwards to it.
The view component (JSP) reads the updated data from the model component and prepare this for the presentation. Then it sends the answer as HTML site back to the browser. The user sees the result of his query.
Struts configurationStruts will be configured with various configuration files. The following files are very important.
web.xml
struts-config.xml
Struts-Tag-Bibliotheken
Properties-Dateien
The illustration will show you, on which places struts use the configuration files.
web.xml
With the web.xml you configure the web server for the struts application. In this file you can set where the web server find the struts-config.xml and some other global properties.
struts-config.xml
The controller calls the business logic or a view with a name. The allocation of the names to the action classes or JSP Files(Views) will be set in the struts-config.xml. The advantage is that you can change the definition of the Workflows (Action Mapping), without using the long class names everytime. If you change a class name, you only have to change the name in the struts-config.xml. You don´t update any other part of your application.
Struts-Tag-Libraries
The Struts-Tag-Libraries, a summary of functions, extends the functionality of JavaServer pages. They support the internationalisation (multi-language) of your web application, and the easy creation of form elements.
Properties files
This files will be used to keep the internationalized textes of your web application. You can create a seperated properties file for each language, which contain all textes of the application.
转自Qhttp://www.laliluna.de
瞬时?/strong>
由new命o开辟内存空间的java对象,
如果没有变量对该对象q行引用,它将被java虚拟机回?
瞬时对象在内存孤立存?它是携带信息的蝲?不和数据库的数据有Q何关联关p?在Hibernate?可通过session的save()或saveOrUpdate()Ҏ瞬时对象与数据库相兌,q将数据对应的插入数据库?此时该瞬时对象{变成持久化对?
持久?/strong>
处于该状态的对象在数据库中具有对应的记录,q拥有一个持久化标识.如果是用hibernate的delete()Ҏ,对应的持久对象就变成瞬时对象,因数据库中的对应数据已被删除,该对象不再与数据库的记录兌.
当一个session执行close()或clear()、evict()之后,持久对象变成q对象,此时持久对象会变成脱对?此时该对象虽然具有数据库识别?但它已不在HIbernate持久层的理之下.
持久对象h如下特点:
1.和session实例兌;
2.在数据库中有与之兌的记?
q?br /> 当与某持久对象关联的session被关闭后,该持久对象{变ؓq对象.当脱对象被重新兌到session上时,q再ơ{变成持久对象.
q对象拥有数据库的识别?可通过update()、saveOrUpdate(){方?转变成持久对?
q对象h如下特点:
1.本质上与瞬时对象相同,在没有Q何变量引用它?JVM会在适当的时候将它回?
2.比瞬时对象多了一个数据库记录标识?
《{?/p>
This time I based the DAO example on interfaces. Tools like Hibernate already provide database portability, so persistence layer portability shouldn't be a driving motivation for interfaces. However, DAO interfaces make sense in more complex applications, when several persistence services are encapsulate in one persistence layer. I'd say that you should use Hibernate (or Java Persistence APIs) directly in most cases, the best reason to use an additional DAO layer is higher abstraction (e.g. methods like getMaximumBid() instead of session.createQuery(...) repeated a dozen times).
I use one interface per persistent entity, with a super interface for common CRUDfunctionality:
The DAO interface for a particular entity extends the generic interface and provides the type arguments:
We basically separate generic CRUD operations and actual business-related data access operations from each other. (Ignore the named query constants for now, they are convenient if you use annotations.) However, even if only CRUD operations are needed for a particular entity, you should still write an interface for it, even it it is going to be empty. It is important to use a concrete DAO in your controller code, otherwise you will face some refactoring once you have to introduce specific data access operations for this entity.
An implementation of the interfaces could be done with any state-management capable persistence service. First, the generic CRUD implementation with Hibernate:
There are some interesting things in this implementation. First, it clearly needs a Session to work, provided with setter injection. You could also use constructor injection. How you set the Session and what scope this Session has is of no concern to the actual DAO implementation. A DAO should not control transactions or the Session scope.
We need to suppress a few compile-time warnings about unchecked casts, because Hibernate's interfaces are JDK 1.4 only. What follows are the implementations of the generic CRUD operations, quite straightforward. The last method is quite nice, using another JDK 5.0 feature, varargs. It helps us to build Criteria queries in concrete entity DAOs. This is an example of a concrete DAO that extends the generic DAO implementation for Hibernate:
We could bring it all together in a DAO factory, which not only sets the Session when a DAO is constructed but also contains nested classes to implement CRUD-only DAOs with no business-related operations:
Note that this factory example is suitable for persistence layers which are primarily implemented with a single persistence service, such as Hibernate or EJB 3.0 persistence. If you have to mix persistence APIs, for example, Hibernate and plain JDBC, the pattern changes slightly. Keep in mind that you can also call session.connection() inside a Hibernate-specific DAO, or use one of the many bulk operation/SQL support options in Hibernate 3.1 to avoid plain JDBC.
Finally, this is how data access now looks like in controller/command handler code (pick whatever transaction demarcation strategy you like, the DAO code doesn't change):
Qhttp://www.hibernate.org