public class DBTest {
public static void main(String args[]) {
try{
Class.forName(\odbc.JdbcOdbcDriver\ }
catch(ClassNotFoundException e) {
System.out.println(\驱动不存在\ } try {
Connection
con=DriverManager.getConnection(\cher\
Statement
stmt=con.createStatement();
String sql=\from teacher\
ResultSet
rs=stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(\编号:\
System.out.println(\姓名:\
System.out.println(\年
Class.forName(\龄:\
odbc.JdbcOdbcDriver\
System.out.println(\性 }
别:\
catch(ClassNotFoundE
xception e)
System.out.println(\职 { 称:\ System.out.println(\驱动 System.out.println(\毕不存在\业院校:\ } try System.out.println(\基 {
本工资:\ Connection
con=DriverManager.getC }
onnection(\
cher\ }
Statement
stmt=con.createStatement catch(SQLException
(); e2)
String sql=\ {
into teacher values(3,'王 System.out.println(\数五','26','男','讲师','陕西师据库操作错误\范大学','9000')\ } int } i=stmt.executeUpdate(sql)} ; DBTest2.java if(i==1) /*1.在Access中建立一个
System.out.println(\插入教师表:
表结构包括的字段有:操作成功\姓名、年龄、性别、职称、 } 毕业院校、基本工资等信
catch(SQLException 息。
e2)
编程实现以下功能:
{
(1) 插入某一教师的具 体信息。*/ //System.out.println(\import java.sql.*; 数据库操作错误\public class DBTest2 {
public static void e2.printStackTrace(); main(String args[]) } { } try{ }
DBTest3.java public class Test /*1.在Access中建立一个System.out.println(\更新{
TextField t_name; 教师表: 操作成功\
TextField t_password; 表结构包括的字段有: }
ResultSet rs;
姓名、年龄、性别、职称、
String sql; catch(SQLException 毕业院校、基本工资等信 String name1; e2)
息。 String password1; { 编程实现以下功能: public Test() (2) 修改某一教师的职 //System.out.println(\ {
Frame f=new 称和基本工资。*/ 数据库操作错误\
Frame(\用户登陆界面\import java.sql.*;
public class DBTest3 { e2.printStackTrace();
f.setBounds(300,400,28 public static void }
0,280); main(String args[]) }
f.setLayout(null); { }
Label name=new try{
Label(\用户名:\Class.forName(\ odbc.JdbcOdbcDriver\ name.setBounds(50,50, } 50,50); Label catch(ClassNotFoundEpassword=new Label(\密xception e) 码:\
{
制作如上图所示的界面: password.setBounds(5System.out.println(\驱动输入用户名和密码,点击0,120,50,50); 不存在\确定则输出“欢迎光临”, t_name=new } 不正确则输出“用户名和TextField(10); try 密码有误”。点击“退出”, { t_name.setBounds(120,则关闭窗体。 Connection 50,100,30);
(1) 用户名和密码在数据库con=DriverManager.getC t_password=new 的UserInfo表中保存。 TextField(10); onnection(\
(2) 请考虑这个过程的各种 cher\
异常,对异常进行处理。 t_password.setEchoCh Statement
stmt=con.createStatement(3) 采用匿名内部类实现事ar('*'); (); 件处理机制,这个过程
String sql=\ 包括ActionEvent和 t_password.setBounds(teacher set 年龄= '80' 120,120,100,30); WindowEvent。
Button where 姓名 ='李四'\
confirm=new Button(\确 int Test.java
i=stmt.executeUpdate(sql)import java.awt.*; 定\; import java.awt.event.*; if(i==1) confirm.setBounds(60,import java.sql.*;
180,50,30);
Button exit=new Button(\退出\
exit.setBounds(150,180,50,30);
f.add(name); f.add(password); f.add(t_name); f.add(t_password); f.add(confirm); f.add(exit);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0); } });
confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName(\bc.odbc.JdbcOdbcDriver\
}
catch(ClassNotFoundException e1)
{
System.out.println (\驱动不存在\ } try { Connection
con=DriverManager.getConnection(\DB\ Statement
stmt=con.createStatement();
name1=t_name.getText();
password1=t_password.getText();
sql=\where
name='\and password='\\
rs=stmt.executeQuery(sql);
if(rs.next())
{
System.out.println (\欢迎光临\
} else {
System.out.println (\用户名和密码有误\ } }
catch(SQLException e2)
{
e2.printStackTrace();
System.out.println (\数据库操作错误\ }
catch(NumberFormatException e1) {
System.out.println(\请输入数据\
} }
}); }
public static void main(String args[]) {
new Test(); } }
实验4 抽象类与接口
一、实验目的
理解抽象类和接口(interface)的概念,学会使用抽象类与接口解决面向对象程序的编写,结合extends和implements完成多重继承。 二、实验要求
7.理解抽象类的定义、以及与普通类的区别。
8.理解接口的定义、以及与抽象类的区别,请注意接口中属性和抽象方法的访问权限及修饰符。 9.熟练掌握抽象类和
接口解决实际编程这三个类要实现接 public void showType() 的能力。 口MyInterface,并且 { 三、实验内容 在测试类中要输出这 System.out.println(\采用抽象类和接口分别解三个对象的面积、周类型为:\
}
决下述问题 长、类型。
}
Test1.java 抽象类: class Circle extends Shape
定义父类(该类是/*抽象类: //定义一个子类Circle继
定义父类(该类是抽象抽象的):Shape 承Shape :Shape 属性 的){ 属性 PI=3.14; PI=3.14; double radius;
(用以表//定义半径 属性 type; 属性 type;
(用以表示类型,例如圆示类型,例如圆型、矩形 Circle(double radius)
等) //用构造方法初始化半径 型、矩形等)
抽象方法: 抽象方法:计算面 {
计算面积(getArea())、积(getArea())、计算周长 this.radius=radius;
计算周长(getZhouchang())
实例方法:输出该 } (getZhouchang())
public double getArea()
实例方法:类的类型。例如圆型就输
//定义方法获得面积 出圆型。 输出该类的类型。例如
{
子类:圆形、矩形、三角圆型就输出圆型。 return
子类:圆形、矩形、型。这三个类需要哪些属PI*radius*radius; 三角型。这三个类需要性自己考虑。 }
哪些属性自己考虑。 定义测试类:分别创建三 public double 定义测试类:分别个子类的对象,并且在测getZhouchang() //定义创建三个子类的对象,试类中要输出这三个对象方法获得周长
{ 并且在测试类中要输的面积、周长和类型。
return 出这三个对象的面积、*/
abstract class Shape //2*PI*radius; 周长和类型。
定义一个父类Shape为抽 } 接口:
} 象类 定义一个接口:class Rectangle extends { MyInterface 定义矩形继承 public double PI=3.14; Shape //
属性:PI=3.1415 Shape //定义属性PI 方法:计算面积{
(getArea())、计算周长 public String type; String (getZhouchang())、输 type=\出类型 public abstract double double width,height; getArea(); //定义抽象方//定义长和宽
Rectangle(double 定义三个类:圆形、法计算面积
正方形、三角型。这三 public abstract double width,double height) 个类需要哪些属性自getZhouchang(); //定 {
this.width=width;
己考虑。 义抽象方法计算周长 this.height=height;
} } public double getArea()}
//定义方法获得public class Test1
{ 面积
public static void {
main(String args []) return
{ width*height;
Circle cir=new }
public double Circle(5); //用CirclegetZhouchang() //定义方创建对象cir
cir.showType(); 法获得周长
System.out.println(\圆 {
形的面积 return
2*(width+height); 为:\.getArea()); } System.out.println(\圆} 形的周长class Sanjiao extends
为:\.getZhouchang());
Shape //定义Sanjiao继 Rectangle rec=new 承Shape Rectangle(2,3); //用{
Rectangle创建对象rec
double a,b,c,s; //
System.out.println(\矩
定义三条边a,b,c
形的面积 Sanjiao(double
a,double b,double c) //定为:\
义构造方法初始化三条边 System.out.println(\矩
形的周长 {
为:\ this.a=a;
; this.b=b;
Sanjiao san=new this.c=c;
Sanjiao(3,4,5); // }
public double getArea() 用Sanjiao创建对象san //定义方法获得面积 System.out.println(\三 { 角形的面积 s=(a+b+c)/2; //为:\
利用海伦公式计算三角形 System.out.println(\三面积 角形的周长 return 为:\Math.sqrt(s*(s-a)*(s-b)*(s; -c)); } } public double } getZhouchang()
输出结果:
{
类型为:null return a+b+c;
圆形的面积为:78.5
圆形的周长为:31.400000000000002 矩形的面积为:6.0 矩形的周长为:10.0 三角形的面积为:6.0 三角形的周长为:12.0 Test1.java /*接口:
定义一个接口:MyInterface
属性:PI=3.1415
方法:计算面积(getArea())、计算周长(getZhouchang())、输出类型
定义三个类:圆形、正方形、三角型。这三个类需要哪些属性自己考虑。 这三个类要实现接口MyInterface,并且在测试类中要输出这三个对象的面积、周长、类型。 */
interface MyInterface //定义MyInterface为接口 {
public double PI=3.14; //定义属性PI
double getArea(); //定义方法计算面积
double getZhouchang(); //定义方法计算周长 }
class Circle implements MyInterface //定义Circle继承MyInterface {
public double radius; //定义半径
Circle(double radius) //用构造方法初始化半径 {