您的位置:首页 > 编程语言 > Java开发

最基本:MyEclipse之HelloWorld

2010-04-20 13:34 155 查看
<!--
/* 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;}
-->

MyEclipse

之HelloWorld



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



 


目录




目的

...
3

2
    
MyEclipse
安装

...
3

2.1
安装软件存放位置

...
3

2.2 MyEclipse
安装说明

...
4

3
    
创建

HelloWorld
工程

...
4

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

...
5

5
    
创建

HelloWorld


web
应用

...
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

 
 

 

      

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


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

 

序号



名称



安装目录



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 > Java
Application
,(若是第一次,则有弹出窗口,点
OK

,
即可运行该程序,运行结果在
Console
窗口中显示,这里即显示
hello World


断点设置

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


Debug As >
Java Application
会进入
debug
模式,如图六所示。此时,可按
F6

F5
单步调试。

 

 

图六

单步调试



 

5     

创建HelloWorld
之web
应用

5.1 

集成Tomcat5
服务器


Window > preferences…

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

jdk
的安装目录,见图九。

图七
 

集成
tomcat5

应用服务器



 

 

 

 

图八
 



eclipse

中启动
tomcat


 

 

 

 

图九
 

设置
tomcat


JDK


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

联接数据库

将联接
sqlserver

jdbc
驱动程序(位于
//100.100.100.250/
实训资料
/sql/jdbc
)拷贝

d:/sqlserver
目录下。

创建
sqlserver
类库,从
Window > preferences…

进入
preference
窗口,如图十三所示,

图十三
 

创建
user library


 

在左边列表中选,
java > Build path > User Libraries

,在右边按
new
命令;创建名为
sqlserver

user library
,之后,在
Add jars
命令加入
sqlserver jdbc
驱动程序的三个
jar
文件。

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

测试程序见图十五。

图十四
 

在工程中加入
user
libraries


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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!");

       }

}

 
      

 

图十五
 

联接数据库测试代码



 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息