1.使用自己的话表述MVC。
MVC(Mode View Controller 模型-视图-控制器)
用于设计创建web应用程序的模式:
模型: 用于判断接收的信息在数据库中是否存在记录
视图:可表示为可看见的内容
控制器:在模型和视图之间传递数据,用于控制用户的注册,登录等操作
2.掌握JDBC的基本操作。
对book表中的数据进行增删改查;
book表:id int,bookName varchar,price int,description varchar
Book类:id int,bookName String ,price int,description String
提示:创建数据库连接工具类DBUtil类。
1.创建数据库表
2.创建Book.class
public class Book {
private String bookName;
private int id;
private int price;
private String description;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Book(){
}
public Book(int id,String bookName,int price,String description){
this.id=id;
this.bookName=bookName;
this.price=price;
this.description=description;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//toString 返回类型
public String toString(){
return "Book [id=" + id + ", bokName=" + bookName + ", price=" + price + ",descript="+description+"]" ;
}
}
3.DBUti.class 创建数据库连接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* 数据库连接工具类
*
*/
public class DBUtil {
//类加载只执行一次
static{
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
String sql="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8";//创建连接
Connection conn;
try {
conn = DriverManager.getConnection(sql,"root","zhangsan");//连接数据库
return conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//关闭连接
public static void close(Connection conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement ps){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.创建VisitMysql.class 设置增删改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entiy.Book;
import com.sql.util.DBUtil;
public class VisitMySql {
private String sql;
private PreparedStatement pstmt;
private Connection conn;
/**
* 增加数据库里面一条书本信息的方法
*
*/
public void Insert(Book b) throws SQLException {
Connection conn = DBUtil.getConnection();//连接数据库
int i = 0;
sql = "INSERT INTO book(id,bookName, price,description) VALUES (?,?,?,?)";//插入到数据库
pstmt =conn.prepareStatement(sql);
pstmt.setInt(1, b.getId());
pstmt.setString(2, b.getBookName());
pstmt.setInt(3, b.getPrice());
pstmt.setString(4, b.getDescription());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
*
* 根据数据库id改书名 价格 描述 的方法
*/
public void Update(int id,Book b) throws SQLException {
Connection conn = DBUtil.getConnection();
int i = 0;
sql ="UPDATE Book SET bookName = '"+b.getBookName()+"',"
+ " price = '"+b.getPrice()+"', description= '"+b.getDescription()+"' WHERE id= "+id;
pstmt =conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
* 根据id删除数据库对象信息的方法
*/
public void Delete(String id) throws SQLException {
Connection conn = DBUtil.getConnection();
int i = 0;
sql ="DELETE FROM Book WHERE id = '"+id+"'";
pstmt =conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
* 根据id进行查询的方法
*/
public void Select(String id) throws SQLException {
Connection conn = DBUtil.getConnection();
sql = "SELECT * FROM Book where id='"+id+"'";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
Book b = new Book();
while (rs.next()) {
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("bookName"));
System.out.println(rs.getInt("price"));
System.out.println(rs.getString("description"));
System.out.println("---------------------------");
}
conn.close();
pstmt.close();
rs.close();
}
public void Select() {
// TODO Auto-generated method stub
}
}
5.创建一个测试类 test.class
import java.sql.SQLException;
import java.util.Scanner;
import com.entiy.Book;
import com.sql.server.VisitMySql;
public class Test {
public static void main(String[] args) throws SQLException {
Scanner sc=new Scanner(System.in);
//录入信息
/*
System.out.println("请输入录入id:");
int id=sc.nextInt();
System.out.println("请输入录入bookName:");
String bookName=sc.next();
System.out.println("请输入录入price:");
String price=sc.nextInt();
System.out.println("请输入录入description:");
String description=sc.next();
Book s1=new Book(id,bookName,price,description);
VisitMySql dl1=new VisitMySql();
dl1.Insert(s1);
dl1.Select();
}
}*/
//修改信息
/* System.out.println("请输入要改的id:");
int id=sc.nextInt();
System.out.println("请输入要改的bookName:");
String bookName=sc.next();
System.out.println("请输入要改的price:");
int price=sc.nextInt();
System.out.println("请输入要改的description:");
String description=sc.next();
Book s2=new Book(id,bookName,price,description);
VisitMySql dl2=new VisitMySql();
dl2.Update(id,s2);
dl2.Select();
}
}
*/
//删除信息
/* System.out.println("请输入您要删除的id:");
String id=sc.nextInt();
VisitMySql dl3=new VisitMySql();
dl3.Delete(id);
dl3.Select();
}*/
//查询信息
/* System.out.println("请输入您要查询的id:");
String id=sc.nextInt();
VisitMySql dl4=new VisitMySql();
dl4.Select(id);
System.out.println("查询成功,信息如下");
}
}*/