Day9 2019-11-11
PreparedStatement
PreparedStatement接口是Statement接口的子接口,它直接继承并重载了Statement的方法。
- 创建PreparedStatement对象形式如下
- String sql=" ";
- PreparedStatement pre=con.prepareStatement(sql);
-
输入参数的赋值
PreparedStatement中提供了大量的setXXX方法对输入参数进行赋值。根据输入参数的SQL类型应选用合适的setXXX方法。
JDBC的工具类
数据库连接类(oracle)
代码如下:
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());
}
}
分层模型类
例子:Student的增删改查
pojo
代码如下:
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;
}
}
dao
代码如下:
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<Student> getAllStudent();
public Student getStudentByStuNo(int stuNo);
public ArrayList<Student> getStudentBySql(String sql);
}
daoImplments
代码如下:
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<Student> getAllStudent() {
ArrayList<Student> list = new ArrayList<Student>();
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<Student> getStudentBySql(String sql) {
ArrayList<Student> list = new ArrayList<Student>();
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;
}
}
tools
在前面 一个连接类 一个日期转化类
HTML基本格式
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css" target="_blank" rel="external nofollow" >-->
<style>
</style>
</head>
<body>
This is a html page.<br>
</body>
</html>
HTML中的标签
- body —通常在这里写主体部分
- <h1></h1> —标题,标题一的字体最大,标题六的字体最小
- <p></p> —段落标签
- <br> —换行
- <hr> —水平线
- <ul></ul> —无序列表
- <ol></ol> —有序列表
- <li></li> —定义列表的项目
表格基本结构
<table> ---表格
<tr> ---表示一行
<th></th> ---定义表头,表示一列,默认加粗,居中
<td></td> ---定义表格单元,表示一列,默认不加粗,居左
<td></td>
<td></td>
</tr>
</table>
text文本域常用属性
- name 文本域的名称 例如:name=“textname”
- maxlength 文本域输入的最大字符 例如:maxlength=“6”
- size 文本域的宽度 例如:size=“10”
- value 文本域的默认值 例如:value=“认真听讲”
- readonly 文本域只读 例如:readonly
选择性输入元素
- select进行项目的多项选择
- 基本属性:
- name 文本域的名称
- size 显示的选项的数目
- multiple 列表中的项目多选
-
基本格式
<select name=“name” multipse>
<option value=“value” selected>选项<option>
<option value=“value”>选项<option>
</select>