Javaweb作业
1.抽象类为什么不能创建对象?
抽象类和接口都能创建对象,只是后边的new不能是new他们自己,但可以new他们的实现类(必须是类,不能是抽象类和接口),
人们使用抽象类和接口只是为了说明要干什么事,而让他们的实现类去根据自己的需要去实现这些方法,比如说抽象类定义一个eat()方法,
它并没有说这个方法具体怎么做,羊就可以吃草,虎就可以吃肉,羊和虎都实现了这个吃的方法。
一个类继承抽象类。用抽象类的子类实例化抽象类的引用。例如
A是抽象类 B extends A ,并且B实现了A中所有的抽象方法,就可以A a = new B();
2.SQL查询语句关键字的执行顺序。
执行顺序是;
from —> on —> outer(join) —> where —> group by
—> cube|rollup —> having —> select —> distinct —> order by —> top
3.Statement和prepareStatement的区别。
语法区别:
1.statement的语法;
Statement stmt = connect.createStatement();
String sql= "SELECT * FROM cg_user WHERE userId=10086 AND name LIKE 'xiaoming'";
ResultSet rs = stmt.executeUpdate(sql);
2.preparestatement的语法;
PreparedStatement preparedStatement = connect.prepareStatement("SELECT * FROM cg_user WHERE userId= ? AND name LIKE ?");
preparedStatement .setInt(1, 10086 );
preparedStatement .setString(2, "xiaoming");
preparedStatement .executeUpdate();
访问数据库的速度:
Statement不会初始化,没有预处理,没次都是从0开始执行SQL,
prepareStatement会先初始化SQL,先把这个SQL提交到数据库中进行预处理,多次使用可提高效率。
preparestatement能批量执行
好处:Update大量的数据时, 先构建一个INSERT语句再多次的执行, 会导致很多次的网络连接.。要减少JDBC的调用次数改善性能, 可以使用PreparedStatement的AddBatch()方法一次性发送多个查询给数据库。
JDBC驱动的最佳化是基于使用的是什么功能. 选择PreparedStatement还是Statement取决于你要怎么使用它们. 对于只执行一次的SQL语句选择Statement是最好的. 相反, 如果SQL语句被多次执行选用PreparedStatement是最好的.
web工程中项目分层:
实体类 ----POJO com.openlab.pojo
数据链路层 ---- Dao com.openlab.dao
业务层 Service ----接口 com.openlab.service
Impl ---- 接口的实现 com.openlab.service.impl
控制层 ----servlet ----controller
JavaBean的实现;
Employee
package xxl;
//领域对象层
public class Employee {
private String id;
private String username;
private double salary;
private int age;
private String depart;
public Employee() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
@Override
public String toString() {
return "Employee [id=" + id + ", username=" + username + ", salary=" + salary + ", age=" + age + ", depart="
+ depart + "]";
}
}
工具类JDBCUtils
public List queryByAll(String sql){
List list = null;
try {
conn = getConnection();
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
list = rsToList(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcClose(conn, st);
}
return list;
}
/**
* 单挑记录的查询 Object
*/
public Object queryByOne(String sql){
Object obj = null;
try {
conn = getConnection();
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
obj = rsToObj(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcClose(conn, st);
}
return obj;
}
public abstract Object rsToObj(ResultSet rs);
public abstract List rsToList(ResultSet rs);
业务逻辑层 Service
package xxl.service;
// 业务层接口
import java.util.List;
import xxl.pojo.Employee;
public interface EmployeeService {
public Employee queryById(String id);
public List queryByUserName(String username);
}
数据链路层Dao
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//数据链路层
import com.xxl.pojo.Employee;
import com.xxl.utils.JDBCUtils;
public class EmployeeServcieDao extends JDBCUtils{
@Override
public Object rsToObj(ResultSet rs) {
Object obj = null;
try {
Employee emp = new Employee();
if(rs.next()){
emp.setId(rs.getString("id"));
emp.setUsername(rs.getString("username"));
emp.setSalary(rs.getDouble("salary"));
emp.setAge(rs.getInt("age"));
emp.setDepart(rs.getString("depart"));
obj = emp;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
@Override
public List rsToList(ResultSet rs) {
List list = new ArrayList();
try {
Employee emp = new Employee();
while(rs.next()){
emp.setId(rs.getString("id"));
emp.setUsername(rs.getString("username"));
emp.setSalary(rs.getDouble("salary"));
emp.setAge(rs.getInt("age"));
emp.setDepart(rs.getString("depart"));
list.add(emp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
业务层IMPL 具体业务逻辑的实现
package xxl.service.impl;
import java.util.List;
import xxl.dao.EmployeeServcieDao;
import xxl.pojo.Employee;
import xxl.service.EmployeeService;
public class EmployeeServiceImpl implements EmployeeService{
EmployeeServcieDao edao = new EmployeeServcieDao();
@Override
public Employee queryById(String id) {
String sql = "select * from tb7 where id='"+id+"'";
Object obj = edao.queryByOne(sql);
Employee em =(Employee) obj;
return em;
}
@Override
public List queryByUserName(String username) {
String sql ="select * from tb7 where username='"+username+"'";
List list = edao.queryByAll(sql);
return list;
}
}
测试类
package xxl.test;
import java.util.List;
import org.junit.Test;
import xxl.pojo.Employee;
import xxl.service.impl.EmployeeServiceImpl;
import junit.framework.TestCase;
public class EmployeeServiceImplTest {
EmployeeServiceImpl eimpl = new EmployeeServiceImpl();
@Test
public void testQueryById() {
Employee e = eimpl.queryById("006");
System.out.println(e);
TestCase.assertEquals("qiweifeng", e.getUsername());
}
@Test
public void testQueryByUserName(){
List list = eimpl.queryByUserName("qiweifeng");
System.out.println(list);
TestCase.assertEquals(2, list.size());
}
}
结果展示:
PrepareStatement用法
package xxb.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class preJDBCTest {
public static void main(String[] args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection conn = JDBCUtils.getConnection();
String sql = "select * from tb7 where username=?";
ps = conn.prepareStatement(sql);//已经对SQL语句进行预处理了
// ? 在这里究竟表示的是什么东 西 要在这个部分描述清楚
//参数索引 在Sql语句中参数索引起始位置是1
// ?所带表的具体的值
ps.setString(1, "' or 1 or '");
// 执行sql语句 该执行方法不需要再传递sql参数
rs = ps.executeQuery();
while(rs.next()){
System.out.println(
rs.getString("id")+"\t"+
rs.getString("username")+"\t"+
rs.getDouble("salary")+"\t"+
rs.getInt("age")+"\t"+
rs.getString("depart"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文地址:https://blog.csdn.net/weixin_45042315/article/details/107326307