Java程序运行机制及运行过程
1.Java虚拟机可以理解成一个以字节码为机器指令的CPU
2.对于不同平台的,有不同的虚拟机
3.Java虚拟机机制屏蔽了底层运行平台的差别,实现“一次编译,随处运行”
*.java文件编译生成 *.class文件然后在各个平台运行
垃圾收集
1.garbage collector
2.不在使用的内存空间应回收
3.Java语言替代了程序员回收无用内存空间的责任;提供了一种系统级线程跟踪存储空间的分配情况。并在JVM的空闲时,检查并释放哪些可被释放的存储器空间。
4.垃圾收集在Java程序运行过程中自动进行,程序员无法精确控制和干预.
windows环境下的安装配置
1.安装JDK软件包
2.设置环境变量
我的电脑–>属性–>高级–>环境变量–>新建JAVA_HOME=JDK
编辑:path=在原来的path路径前增加JDK安装目录/bin
测试 运行cmd
java javac java -version 这三个命令都能运行,则安装成功
DJK安装目录树介绍
Bin 编译器和工具
Demo 演示
sample JDK自带的实例代码
Includ 用于编译本地方法的文件
Jre Java运行环境文件
Lib 类库文件
Src 类库源文件
2. Java Application初步
3.Java SE之基础语法
为什么可以跨平台?
8种基本数据类型
数字类型
1.整数 byte 8位 short 16位 int 32位 long 64位
2.浮点数 float 32位 double 64位
布尔类型 boolean
字符类型 char
其余的为引用类型 类(class);接口(interface);数组等。
运算符
-逻辑运算符: &&(短路与),第一条件为false,后边不运行
&(与),第一条件为false,后边还会运行
||(短路或),第一条件为true,后边不运行
|(或),第一条件为true,后边还会运行
循环(均有初始化的值)
1.for 容易确定循环次数
2.do while 先做后循环
3.while
学生管理系统,来管理学生
现实生活中存在的对像(学生,老师,宿舍)
观察—》抽取—》现实生活中存在的对像(看得到的,摸得到,能描述的)–
分类–,抽像出这个类别的行为、特征----
学生类别
{
姓名 特征
年纪 特征
学习行为() 行为
{
}
java
class Student
{
String name; 属性
int age;属性
public void show() 方法
{
system.out.println(“我叫”+name);
}
}
写类不是最终的目的,
new Student(“张三”,10);
new Student(“张1”,12);
new Student(“张2”,10);
new Student(“张3”,10);
1.二维表数组
2.数组的数组
类就是一个模板,有了这个模板才能创建对象。
抽象的数据类型,用来描述某一类具有相同特性的对象。
-隐藏实现细节
-为用户提供统一界面(get()和set()方法)
-增强可维护性
this:指向当前类的对象
代码如下:
/**
* 学生的姓名,年龄
* @author FDL
*
*/
public class Student {
private String name;
private int age;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
/**
* 构造方法重载,无参
*/
public Student() {
super();
}
/**
* 构造方法重载,有参
*/
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
getAge();
this.age = age;
}
}
/**
* 测试交换
* @author FDL
*
*/
public class TestStudent {
public static void main(String[] args) {
Student stu=new Student("王为峰", 12);
TestStudent testStudent=new TestStudent();
testStudent.change(stu);//也就是stu和stu2指向同一片内存区域,当stu2改变值 时,stu在取值就得到改变后的值
System.out.println(stu);
System.out.println();
}
public void change(Student stu2) //对像方法, 参数是引用类型,它传的是地址,也就是stu和stu2指向同一片内存区域
{
stu2.setName("wangwei_feng");
stu2.setAge(21);
}
}
1.首先需要抽取问题描述中对象
2.分析每个对象所具有的特征
3.分析每个对象所发出的动作
4.从这些对象的特征中,抽取类的属性和方法
5.分析类之间的关系,画出类结构图
抽象类和对象的基本方法:
Animal.java
package FDL02;
public abstract class Animal {
protected boolean mammal=true; // 哺乳动物
protected boolean carnivorous=true; //食肉动物
public static final int scared=0; //情绪不好
public static final int comforted=1; //情绪好
protected int mood=comforted;
public boolean ismammal() {
return (mammal);
}
public boolean iscarnivorous() {
return (carnivorous);
}
abstract String SayHello();
abstract String SayHello(int moodval);
public void setMood(int newValue)
{
mood=newValue;
}
public int getMood()
{
return(mood);
};
}
LandAnimal.java
package FDL02;
public interface LandAnimal {
public int getNumberOfLegs();
}
WaterAnimal.java
package FDL02;
public interface WaterAnimal {
public boolean isGillFlag();//返回是否有腮
public boolean isLaysEggs();//返回是否产卵
}
Cat.java
package FDL02;
public class Cat extends Animal implements LandAnimal{
private int numberOfLegs = 4;
public String SayHello() {
return "喵喵叫";
};
public String SayHello(int moodval) {
this.setMood(moodval);
switch (mood) {
case scared:
return ("嘶嘶");
case comforted:
return ("咕噜咕噜");
}
return ("喵喵叫");
}
public int getNumberOfLegs() {
return numberOfLegs;
}
}
Dog.java
package FDL02;
public class Dog extends Animal implements LandAnimal{
private int numberOfLegs = 4;
public String SayHello() {
return "摇摇尾巴";
};
public String SayHello(int moodval) {
this.setMood(moodval);
switch (mood) {
case scared:
return ("呜呜叫");
case comforted:
return ("汪汪叫");
}
return ("摇摇尾巴");
}
public int getNumberOfLegs() {
return numberOfLegs;
}
}
Frog.java
package FDL02;
public class Dog extends Animal implements LandAnimal{
private int numberOfLegs = 4;
public String SayHello() {
return "摇摇尾巴";
};
public String SayHello(int moodval) {
this.setMood(moodval);
switch (mood) {
case scared:
return ("呜呜叫");
case comforted:
return ("汪汪叫");
}
return ("摇摇尾巴");
}
public int getNumberOfLegs() {
return numberOfLegs;
}
}
Main.java
package FDL02;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Dog dog = new Dog();
Cat cat = new Cat();
Frog frog = new Frog();
System.out.println("~欢迎光临动物园~ ");
for (;;) {
System.out.println("请输入动物名称:dog,cat或者frog ,退出exit!");
String name = new String();
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
name = sc.next();
switch (name) {
case "dog":
System.out
.println("该动物是陆生动物,它有" + dog.getNumberOfLegs() + "条腿");
System.out.println("狗通常情况下,跟人的打招呼方式为:" + dog.SayHello());
break;
case "cat":
System.out
.println("该动物是陆生动物,它有" + cat.getNumberOfLegs() + "条腿");
System.out.println("猫通常情况下,跟人的打招呼方式为:" + cat.SayHello());
break;
case "frog":
if (frog.isLaysEggs() && frog.isGillFlag())
System.out.println("该动物是陆生动物,它有" + frog.getNumberOfLegs()
+ "条腿。" + "有腮" + "," + "产卵");
System.out.println("青蛙通常情况下,跟人的打招呼方式为:" + frog.SayHello());
break;
case "exit":
System.out.println("再见!");
System.exit(0);
default:
System.out.println("输入有误 !请重新输入!");
}
System.out.println("请输入动物的心情:1-心情好,2-心情不好");
int i;
@SuppressWarnings("resource")
Scanner sc1 = new Scanner(System.in);
i = sc1.nextInt();
switch (i) {
case 1:
System.out.println("它心情是:心情好");
break;
case 2:
System.out.println("它心情是:心情不好");
break;
default:
System.out.println("输入有误 !请重新输入!");
}
switch (name) {
case "dog":
if (i == 1) {
System.out
.println("狗心情好:" + dog.SayHello(Animal.comforted));
} else if (i == 2) {
System.out.println("狗心情不好:" + dog.SayHello(Animal.scared));
} else {
System.out.println("输入错误!");
}
break;
case "cat":
if (i == 1) {
System.out
.println("猫心情好:" + cat.SayHello(Animal.comforted));
} else if (i == 2) {
System.out.println("猫心情不好:" + cat.SayHello(Animal.scared));
} else {
System.out.println("输入错误!");
}
break;
case "frog":
if (i == 1) {
System.out.println("青蛙心情好:"
+ frog.SayHello(Animal.comforted));
} else if (i == 2) {
System.out
.println("青蛙心情不好:" + frog.SayHello(Animal.scared));
} else {
System.out.println("输入错误!");
}
break;
default:
System.out.println("输入有误 !请重新输入!");
break;
}
}
}
}
### 注释: ###
1.Introduceable.java
如果您要将输入的字符串转换为整型、浮点数等等数据类型,您可以使用以下各包装类别所提供的各个静态剖析方法:
如果指定的字符串无法剖析为指定的数据类型数值,则会发生NumberFormatException异常。
推荐使用equals比较string类型的 不推荐使用==
public class StringTest1{
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2);
String str3 = new String(“abc");
String str4 = new String(“abc");
System.out.println(str1 == str3);
System.out.println(str3 == str4);
}
}
DateTest.java
public class DateTest {
public static void main(String[] args) {
//java.util.Date utilDate;
//java.sql.Date sqlDate;
//得到系统当前时间日期
java.util.Date utilDate =new Date();
System.out.println(utilDate);
//得到日期格式化类
java.text.SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
String strDate=sdf.format(utilDate); //完成将java.util.Date转成字符串
System.out.println(strDate);
String sDate="2019年11月04 16:12:01";
java.util.Date strToDate=null;
try {
//用日期格式化类将字符串转java.util.Date
strToDate=sdf.parse(sDate);
System.out.println(strToDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//java.util.Date utilDate;
long longDate=strToDate.getTime();
System.out.println(longDate);
//将java.sql.Date转java.util.Date
java.sql.Date d=new java.sql.Date(longDate);
//java.util.Date转java.sql.Date
java.util.Date dd=new java.util.Date(d.getTime());
}
}
Tue Nov 05 08:49:26 CST 2019
2019年11月05 08:49:26
Mon Nov 04 16:12:01 CST 2019
1572855121000
2019-11-04
Mon Nov 04 16:12:01 CST 2019
更新日期:2019/11/5 星期二 9:06:07
public int compareTo(Object o) {
//将此对象与指定的对象进行比较以进行排序。
//返回一个负整数,零或正整数,因为该对象小于,等于或大于指定对象。
Student stu=(Student)o;
int i=0;
if(this.age==stu.age)
{i=0;}
if(this.age>stu.age)
{i=1;}
if(this.age
set可以放一个null,但是不能够放入重复的元素
Address.java
/**
*
* @author FDL
*
*/
public class Address {
private String name;
private String street;
private String city;
private String province;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "[\n姓名:" + name + "\n街道:" + street + "\n市(县):" + city
+ "\n省(自治区):" + province + "\n国家(地区):" + country + "\n]";
}
public Address(String name, String street, String city, String province,
String country) {
super();
this.name = name;
this.street = street;
this.city = city;
this.province = province;
this.country = country;
}
public Address() {
super();
}
}
TestAddress.java
import java.util.ArrayList;
import java.util.List;
public class TestAddress {
public static void main(String[] args) {
List addressList =new ArrayList();
Address a1=new Address("张三", "王府井", "北京", "北京", "中国");
Address a2=new Address("李四", "张扬路", "上海", "上海", "中国");
Address a3=new Address("John lenon", "12 Park Avenue", "California", "CaliforniaState", "USA");
addressList.add(a1);
addressList.add(a2);
addressList.add(a3);
for (Address address : addressList) {
System.out.println(address);
}
}
}
运行结果:
2.构建描述学生的类Student, 属性:姓名和年龄
构建集合来存放学生对象信息,但要保证学生的年龄按大小顺序进行排序
最后遍历输出格式如下图所示
提示:
Student2.java
public class Student2 implements java.lang.Comparable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student2(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student2() {
super();
}
@Override
public String toString() {
return " " + name + " " + age + " ";
}
@Override
public int compareTo(Object o) {
//将此对象与指定的对象进行比较以进行排序。
//返回一个负整数,零或正整数,因为该对象小于,等于或大于指定对象。
//基础是二叉树
Student2 stu=(Student2)o;
//由大到小排列
int i=0;
if(this.age==stu.age) //不处理
{
i=0;// 相等 不处理
}
if(this.agestu.age)
{
i=-1;//大于 往左放
}
return i;
}
}
TestStudent2.java
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class TestStudent2 {
public static void main(String[] args) {
TreeSet tree = new TreeSet();
tree.add(new Student2("Linda", 21));
tree.add(new Student2("Mary", 28));
tree.add(new Student2("John", 26));
tree.add(new Student2("Tom", 25));
tree.add(new Student2("Jack", 24));
tree.add(new Student2("Lisa", 22));
show(tree);
}
public static void show(Set set) {
System.out.println("---------------------------");
System.out.println(" 姓名年龄");
for (Student2 s : set) {
System.out.println("---------------------------");
System.out.println(s);
}
System.out.println("---------------------------");
}
}
根据异常的类型,抛出不同类型的异常
TryCatchTest.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryCatchTest {
public static void main(String[] args) {
System.out.println("请输入1-5之间的数字进行选课:");
Scanner in=new Scanner(System.in);
try{
int courseCode =in.nextInt();//从键盘输入数
switch (courseCode) {
case 1:
System.out.println("C语言");
break;
case 2:
System.out.println("J2EE");
break;
case 3:
System.out.println("数据库");
break;
case 4:
System.out.println("计算机网络");
break;
case 5:
System.out.println("JSP");
break;
}
}catch(InputMismatchException e){
System.out.println("输入类型不对,请输入数字!");
System.out.println("发生错误:"+e);
}catch(Exception e){
System.out.println("发生错误:"+e.getMessage());
e.printStackTrace();
}finally{
System.out.println("结束");
}
}
}
如果在当前环境无法解决一个异常,就要将其抛出,把异常交给调用者处理
1.select 列名(此时不区分大小写) from 表名;
注释
2.条件查询
3.排序
1.select 列名(此时不区分大小写) from 表名;
注释
2.条件查询
3.排序
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
JDBC API 供程序员调用的类和接口被包装在java.sql中如
DriverManager类
Connection接口
Statement接口
ResultSet接口
DriverManager(作用:载入不同的JDBC驱动)
JDBC驱动(作用:负责连接各种不同的数据库)
DriverManager :依据数据库的不同,管理JDBC驱动
Connection :负责连接数据库并担任传送数据的任务
Statement :由 Connection 产生、负责执行SQL语句
ResultSet:负责保存Statement执行后所产生的查询结果
Statement接口用于执行不带参数的简单SQL语句,用来向数据库提交SQL语句交返回SQL语句的执行结果。
创建一个Statement接口的一般形式如下:
Connectioncon=DriverManager.getConnection(URL,“user”,“password”);
con=DriverManager.getConnection(URL,“user”,“password”)
ResultSet 提供检索不同类型字段的方法,常用的有:
ResultSet 还提供了对结果集进行滚动的方法:
public class TestJdbc {
public static void main(String[] args) {
// 1.准备好数据库的连接字符串
String url = "jdbc:oracle:thin:@192.168.1.126:1521:xe";
String driClass = "oracle.jdbc.OracleDriver";
String userName = "fdl";
String userPwd = "fdl";
// 2.在创建的项目里添加驱动类
Connection conn = null;
try {
// 3.将加驱动类加载到内存中
Class.forName(driClass);
// 4.w使用驱动管理类来管理,可以通过驱动管理类来得到连接对像
conn = DriverManager.getConnection(url, userName, userPwd);
// 5,使用连接对像,得到处理器
Statement stat = conn.createStatement();
// 6 使用处理器,来发送sql语句
// stat.executeUpdate(sql) insert update delete 用这个,返加一个int影响的行数
// stat.executeQuery(sql) select语句,返回一个结果集
String insertSql="insert into student(stuNo,stuName,stuBirthday,stuAddress,stuSex) values(SNO.NEXTVAL,'bbb','6-11月-19','巢湖','女')";
//String insertSql = "delete student where stuname='bbb'";
int i = stat.executeUpdate(insertSql);
if (i > 0) {
System.out.println("insert ok");
} else {
System.out.println("insert ng");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
PreparedStatement接口是Statement接口的子接口,它直接继承并重载了Statement的方法。
创建PreparedStatement对象形式如下
输入参数的赋值
PreparedStatement中提供了大量的setXXX方法对输入参数进行赋值。根据输入参数的SQL类型应选用合适的setXXX方法。
代码如下:
package com.fdl.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManage {
public static java.sql.Connection getConn()
{
String url="jdbc:oracle:thin:@192.168.1.126:1521:xe";
String driClass="oracle.jdbc.OracleDriver";
String userName="fdl";
String userPwd="fdl";
Connection conn=null;
try {
Class.forName(driClass);
conn = DriverManager.getConnection(url, userName, userPwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeConn(java.sql.Connection conn)
{
if(conn!=null)
{
try {
if(!conn.isClosed())
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
代码如下:
package com.fdl.common;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Tools {
//SQL类型的Date转化为String类型的
public static String fmtDateToString(java.sql.Date sqlDate)
{
java.util.Date uDate= fmtDateToUtilDate(sqlDate);
java.text.SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(uDate) ;
}
//SQL类型的Date转化为Util类型的Date
public static java.util.Date fmtDateToUtilDate(java.sql.Date sqlDate)
{
return new java.util.Date(sqlDate.getTime());
}
//Util类型的Date转化为SQL类型的Date
public static java.sql.Date fmtDateTosqlDate(java.util.Date lDate)
{
return new java.sql.Date(lDate.getTime());
}
}
代码如下:
package com.fdl.pojo;
import java.util.Date;
public class Student {
private int stuNo;
private String stuSex;
private String stuAddress;
private String stuName;
private Date stuBirthday;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Date getStuBirthday() {
return stuBirthday;
}
public void setStuBirthday(Date stuBirthday) {
this.stuBirthday = stuBirthday;
}
public Student(int stuNo, String stuSex, String stuAddress, String stuName, Date stuBirthday) {
super();
this.stuNo = stuNo;
this.stuSex = stuSex;
this.stuAddress = stuAddress;
this.stuName = stuName;
this.stuBirthday = stuBirthday;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [stuNo=" + stuNo + ", stuSex=" + stuSex + ", stuAddress=" + stuAddress + ", stuName=" + stuName
+ ", stuBirthday=" + stuBirthday + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((stuAddress == null) ? 0 : stuAddress.hashCode());
result = prime * result + ((stuBirthday == null) ? 0 : stuBirthday.hashCode());
result = prime * result + ((stuName == null) ? 0 : stuName.hashCode());
result = prime * result + stuNo;
result = prime * result + ((stuSex == null) ? 0 : stuSex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (stuAddress == null) {
if (other.stuAddress != null)
return false;
} else if (!stuAddress.equals(other.stuAddress))
return false;
if (stuBirthday == null) {
if (other.stuBirthday != null)
return false;
} else if (!stuBirthday.equals(other.stuBirthday))
return false;
if (stuName == null) {
if (other.stuName != null)
return false;
} else if (!stuName.equals(other.stuName))
return false;
if (stuNo != other.stuNo)
return false;
if (stuSex == null) {
if (other.stuSex != null)
return false;
} else if (!stuSex.equals(other.stuSex))
return false;
return true;
}
}
代码如下:
package com.fdl.dao;
import java.util.ArrayList;
import com.fdl.pojo.Student;
public interface IStudentDao {
public boolean insertStudent(Student stu);
public boolean updateStudent(Student stu);
public boolean deleteStudent(int stuNo);
public ArrayList getAllStudent();
public Student getStudentByStuNo(int stuNo);
public ArrayList getStudentBySql(String sql);
}
代码如下:
package com.fdl.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import com.fdl.common.ConnectionManage;
import com.fdl.common.Tools;
import com.fdl.dao.IStudentDao;
import com.fdl.pojo.Student;
public class StudentDaoImpl implements IStudentDao {
@Override
public boolean insertStudent(Student stu) {
boolean b = false;
Connection conn = ConnectionManage.getConn();
String sql = "insert into student(stuNo,stuName,stuBirthday,stuAddress,stuSex) values(SNO.NEXTVAL,?,?,?,?)";
try {
PreparedStatement pre = conn.prepareStatement(sql);
// stuName,
pre.setString(1, stu.getStuName());
// stuBirthday,
pre.setDate(2, Tools.fmtDateTosqlDate(stu.getStuBirthday()));
// stuAddress,
pre.setString(3, stu.getStuAddress());
// stuSex
pre.setString(4, stu.getStuSex());
b = pre.executeUpdate() > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return b;
}
@Override
public boolean updateStudent(Student stu) {
boolean b = false;
Connection conn = ConnectionManage.getConn();
String sql = " update student set stuAddress=?,stuName=?,stuBirthday=?,stuSex=? WHERE stuno=?";
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1, stu.getStuAddress());
pre.setString(2, stu.getStuName());
pre.setDate(3, Tools.fmtDateTosqlDate(stu.getStuBirthday()));
pre.setString(4, stu.getStuSex());
pre.setInt(5, stu.getStuNo());
b = pre.executeUpdate() > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return b;
}
@Override
public boolean deleteStudent(int stuNo) {
boolean b = false;
Connection conn = ConnectionManage.getConn();
String sql = "delete student where stuNo=?";
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1, stuNo);
b = pre.executeUpdate() > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return b;
}
@Override
public ArrayList getAllStudent() {
ArrayList list = new ArrayList();
Connection conn = ConnectionManage.getConn();
String sql = "select * from student";
try {
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()) {
Student stu = new Student();
// stuNo number primary key,
int stuNo = rs.getInt("stuNo");
stu.setStuNo(stuNo);
// stuName nvarchar2(10) not null,
String stuName = rs.getString("stuName");
stu.setStuName(stuName);
// stuBirthday date,
java.sql.Date stuBirthday = rs.getDate("stuBirthday");
stu.setStuBirthday(Tools.fmtDateToUtilDate(stuBirthday));
// stuSex Nchar(1) default '男',
String stuSex = rs.getString("stuSex");
stu.setStuSex(stuSex);
// stuAddress nvarchar2(50)
String stuAddress = rs.getString("stuAddress");
stu.setStuAddress(stuAddress);
list.add(stu);
}
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return list;
}
@Override
public Student getStudentByStuNo(int stuNo) {
Student stu = null;
Connection conn = ConnectionManage.getConn();
String sql = "select * from student where stuNo=?";
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1, stuNo);
ResultSet rs = pre.executeQuery();
if (rs.next()) {
stu = new Student();
// stuNo number primary key,
int stuNo1 = rs.getInt("stuNo");
stu.setStuNo(stuNo1);
// stuName nvarchar2(10) not null,
String stuName = rs.getString("stuName");
stu.setStuName(stuName);
// stuBirthday date,
java.sql.Date stuBirthday = rs.getDate("stuBirthday");
stu.setStuBirthday(Tools.fmtDateToUtilDate(stuBirthday));
// stuSex Nchar(1) default '男',
String stuSex = rs.getString("stuSex");
stu.setStuSex(stuSex);
// stuAddress nvarchar2(50)
String stuAddress = rs.getString("stuAddress");
stu.setStuAddress(stuAddress);
}
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return stu;
}
@Override
public ArrayList getStudentBySql(String sql) {
ArrayList list = new ArrayList();
Connection conn = ConnectionManage.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()) {
Student stu = new Student();
// stuNo number primary key,
int stuNo = rs.getInt("stuNo");
stu.setStuNo(stuNo);
// stuName nvarchar2(10) not null,
String stuName = rs.getString("stuName");
stu.setStuName(stuName);
// stuBirthday date,
java.sql.Date stuBirthday = rs.getDate("stuBirthday");
stu.setStuBirthday(Tools.fmtDateToUtilDate(stuBirthday));
// stuSex Nchar(1) default '男',
String stuSex = rs.getString("stuSex");
stu.setStuSex(stuSex);
// stuAddress nvarchar2(50)
String stuAddress = rs.getString("stuAddress");
stu.setStuAddress(stuAddress);
list.add(stu);
}
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionManage.closeConn(conn);
}
return list;
}
}
在前面 一个连接类 一个日期转化类
index.html
This is a html page.
---表格
---表示一行
---定义表头,表示一列,默认加粗,居中
---定义表格单元,表示一列,默认不加粗,居左
ID值唯一 name值可以不唯一
border
background
request.setCharacterEncoding("utf-8"); //设置请求对象的字符集编码
response.setCharacterEncoding("utf-8");//设置响应对象的字符集编码
response.setContentType("text/html;charset=utf-8");//指导浏览器响应编码
Servlet 的生命周期包含三个阶段
自定义的 servlet 要继承 HttpServlet
请求和响应可按两种方式进行: