最基本:MyEclipse之HelloWorld

来源:互联网 发布:mac如何剪切文件夹 编辑:程序博客网 时间:2024/06/05 05:28

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:黑体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimHei;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:1 135135232 16 0 262144 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@黑体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:1 135135232 16 0 262144 0;}@font-face{font-family:仿宋_GB2312;panose-1:2 1 6 9 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:modern;mso-font-pitch:fixed;mso-font-signature:1 135135232 16 0 262144 0;}@font-face{font-family:"/@仿宋_GB2312";panose-1:2 1 6 9 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:modern;mso-font-pitch:fixed;mso-font-signature:1 135135232 16 0 262144 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}h1{mso-style-next:正文;margin-top:17.0pt;margin-right:0cm;margin-bottom:16.5pt;margin-left:0cm;text-align:justify;text-justify:inter-ideograph;line-height:240%;mso-pagination:lines-together;page-break-after:avoid;mso-outline-level:1;font-size:22.0pt;font-family:"Times New Roman";mso-font-kerning:22.0pt;}h2{mso-style-next:正文;margin-top:13.0pt;margin-right:0cm;margin-bottom:13.0pt;margin-left:0cm;text-align:justify;text-justify:inter-ideograph;line-height:173%;mso-pagination:lines-together;page-break-after:avoid;mso-outline-level:2;font-size:16.0pt;font-family:Arial;mso-fareast-font-family:黑体;mso-bidi-font-family:"Times New Roman";mso-font-kerning:1.0pt;}p.MsoToc1, li.MsoToc1, div.MsoToc1{mso-style-update:auto;mso-style-noshow:yes;mso-style-next:正文;margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;text-indent:21.0pt;mso-char-indent-count:2.0;line-height:21.0pt;mso-pagination:none;tab-stops:42.0pt right dotted 414.8pt;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;mso-no-proof:yes;}p.MsoToc2, li.MsoToc2, div.MsoToc2{mso-style-update:auto;mso-style-noshow:yes;mso-style-next:正文;margin-top:0cm;margin-right:0cm;margin-bottom:0cm;margin-left:21.0pt;margin-bottom:.0001pt;mso-para-margin-top:0cm;mso-para-margin-right:0cm;mso-para-margin-bottom:0cm;mso-para-margin-left:2.0gd;mso-para-margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;tab-stops:84.0pt right dotted 414.8pt;font-size:15.0pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;mso-no-proof:yes;}p.MsoHeader, li.MsoHeader, div.MsoHeader{margin:0cm;margin-bottom:.0001pt;text-align:center;mso-pagination:none;tab-stops:center 207.65pt right 415.3pt;layout-grid-mode:char;border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0cm;mso-padding-alt:0cm 0cm 1.0pt 0cm;font-size:9.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0cm;margin-bottom:.0001pt;mso-pagination:none;tab-stops:center 207.65pt right 415.3pt;layout-grid-mode:char;font-size:9.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoBodyText, li.MsoBodyText, div.MsoBodyText{margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}a:link, span.MsoHyperlink{color:blue;text-decoration:underline;text-underline:single;}a:visited, span.MsoHyperlinkFollowed{color:purple;text-decoration:underline;text-underline:single;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;} /* List Definitions */ @list l0{mso-list-id:660236378;mso-list-type:hybrid;mso-list-template-ids:-23397618 -726891586 -1323645858 -703922540 755649044 1443277384 837682804 -825967726 1867958578 96758554;}@list l0:level1{mso-level-start-at:2;mso-level-text:%1;mso-level-tab-stop:22.5pt;mso-level-number-position:left;margin-left:22.5pt;text-indent:-22.5pt;}@list l0:level2{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level3{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level4{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level5{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level6{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level7{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level8{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l0:level9{mso-level-start-at:0;mso-level-number-format:none;mso-level-text:"";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:0cm;text-indent:0cm;}@list l1{mso-list-id:687484470;mso-list-type:hybrid;mso-list-template-ids:-2083893142 -696615330 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}@list l1:level1{mso-level-text:%1、;mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:18.0pt;text-indent:-18.0pt;}@list l2{mso-list-id:1651135420;mso-list-type:hybrid;mso-list-template-ids:-500644216 -1742941690 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}@list l2:level1{mso-level-start-at:5;mso-level-text:%1;mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:18.0pt;text-indent:-18.0pt;}ol{margin-bottom:0cm;}ul{margin-bottom:0cm;}-->

MyEclipseHelloWorld

(好多图片上传不上去,郁闷)

 

目录

目的... 3

2     MyEclipse安装... 3

2.1 安装软件存放位置... 3

2.2 MyEclipse安装说明... 4

3     创建HelloWorld工程... 4

4 HelloWorld.java之创建、调试、运行... 5

5     创建HelloWorldweb应用... 7

5.1  集成Tomcat5服务器... 7

5.2 创建web应用... 9

5.3 创建jsp页面... 9

5.4 发布与运行... 10

6 联接数据库... 11

 

 

 

 

 

 

 

 

 

 

 

 

目的

本文档用于指导eclipse初学者安装Myeclipse相关软件,并示例说明eclipse之相关使用。

2        MyEclipse安装

2.1 安装软件存放位置

       本文档所述软件存放于局域网上名为software或 “//100.100.100.21的机器上,用户可以在浏览器的地址上输入下述命令,按回车键后进入该机器:

  

//software

  

 


      

       在提示输入用户名与密码时,分别输入aa

    下表是相关软件的存放位置:

 

序号

名称

安装目录

1

Jdk1.5

//100.100.100.250/software/开发工具/jdk-1_5_0-windows-i586.exe

2

Tomcat5.5

//100.100.100.250/software/开发工具/jakarta-tomcat-5.5.9.exe

3

Myeclipse

//100.100.100.250/公用文件夹/项目经理/公用/myeclipse/

 

图一 Myeclipse及相关软件使用说明

2.2 MyEclipse安装说明

1、   解压缩eclipse包至一目录,如d:/

2、   运行MyEclipse的安装程序,在安装时,要将eclipse的目录,指向第一步中安装的eclipse安装目录;

3、   运行注册码程序,在命令行中,执行

      

java  Keygen

  


       输入姓名后,会自动生成注册码。

4、在需要输入姓名及注册码时,输入以上姓名及注册码。

3        创建HelloWorld工程

在使用eclipse之前,请先在E创建一个存放工程文件的目录(e:/eclipse),这个目录也就是eclipse的工作空间(workspace)。注意,我们所有的程序及文档均应放在E盘(或D盘)的指定目录中,不放在C盘中,以免重做系统时,导致文件丢失。

 

图二 eclipse的初始界面

      

初始打开MyEclipse时(或选择workspace时),会显示如图二所示的界面,在其中输入workspace的路径,并按确定即可进入MyEclipse

File > project进入New Project界面,如图三所示,选java Project, 创建一般java工程。

 

 

图三、新建java工程

 

next按钮,进入New Java Project窗口,在Project Name栏中输入工程名helloworld,安finish按钮,完成工程创建。

 

4 HelloWorld.java之创建、调试、运行

右键工程名helloworld,在弹出的上下文菜单中,选new > class,则进入创建类窗口,如图四所示,输入包名hello,类名HelloWorld

 

 

 

 

图四、创建HelloWorld

 

       HelloWorld类中,输入相关代码,如图五所示。

  

public static void main(String[] args) {

              // TODO Auto-generated method stub

              System.out.println("Hello World!");

}

  

 

 

 

 

 


图五、HelloWorld

右键HelloWorld类名,在弹出菜单中,选Run As > JavaApplication,(若是第一次,则有弹出窗口,点OK,即可运行该程序,运行结果在Console窗口中显示,这里即显示hello World

断点设置,在HelloWorld.java窗口,点击左边栏,所出现的小圆点,表明该行是断点,设置断点后,即可进行单步调试。

Debug As >Java Application会进入debug模式,如图六所示。此时,可按F6F5单步调试。

 

 

图六 单步调试

 

5     创建HelloWorldweb应用

5.1  集成Tomcat5服务器

Window > preferences…进入preference窗口,如图七所示,在左边列表中,选MyEclipse>ApplicationServers > Tomcat5,在右边窗口中,选中enable ,并在Tomcat Home Directory中选tomcat5的安装目录,按OK结束设置。如设置正确,可在工具栏中启动tomcat5,如图八所示。注意,如果tomcat5不能正常启动的话,则应正确设置图七tomcat5下的jdkjdk的安装目录,见图九。

图七  集成tomcat5应用服务器

 

 

 

 

图八 eclipse中启动tomcat

 

 

 

 

图九  设置tomcatJDK

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.2 创建web应用

File > Project进入New Project窗口,在该窗口中,选中MyEclipse > J2ee Projects > Web Project,进入创建web project窗口,如图十所示。

 

图十  创建web工程

 

       project Name中输入工程名helloworldweb,按Finish完成创建。注意,将你的web应用文档放在合适的目录下,如e:/eclipse,而不要放在c盘中。

 

5.3 创建jsp页面

右键helloworldweb工程名下的webroot子目录,在上下文菜单中,选New>Jsp进入创建jsp页面,如图十一所示。

 


图十一 创建jsp页面

 

File name中输入hello.jsp,注意,在Template to use中选Just as HTML,按Finish完成创建。

5.4 发布与运行

在工具栏中,按Deploy…按钮进入发布窗口,如图十二所示,在project name中选helloworldweb工程名,并将之发布到tomcat5服务器上。

图十二 发布web应用

 

正常发布后,启动tomcat5,此时,可在浏览器的地址栏中,通过下面的url运行hello.jsp

http://localhost:8080/helloworldweb/hello.jsp

 

 

6 联接数据库

将联接sqlserverjdbc驱动程序(位于//100.100.100.250/实训资料/sql/jdbc)拷贝 d:/sqlserver目录下。

创建sqlserver类库,从Window > preferences…进入preference窗口,如图十三所示,

图十三  创建user library

 

在左边列表中选,java > Build path > User Libraries,在右边按new命令;创建名为sqlserveruser library,之后,在Add jars命令加入sqlserver jdbc驱动程序的三个jar文件。

在工程中加入刚才创建的类库,右键工程名,在上下文菜单中,选Properties进入工程属性窗口,如图十四所示,在左边选java build path,在右边选libraries > Add library,在工程中加入已建的user library,按OK命令。

测试程序见图十五。

图十四  在工程中加入userlibraries

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

public class DBConnection {

       // 联接数据库的驱动程序,缺省值

       private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

       // 联接数据库的URL,缺省值

       private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";

       // 联接数据库的用户与口令

       private String user = "sa";

       private String password = "sa";

 

       public DBConnection() throws Exception {

              // 装载驱动程序

              Class.forName(driverName).newInstance();

       }

 

       public Connection getConnection() throws SQLException {

              return DriverManager.getConnection(url, user, password);

       }

 

       public static void main(String[] args) throws Exception {

              DBConnection dbconnection = new DBConnection();

              dbconnection.getConnection();

              System.out.println("Connection OK!");

       }

}

       

 

图十五  联接数据库测试代码

 

 

原创粉丝点击