记录一下2023.02.13

 

 

 

 

今天是开学第一天,下午就进行了javaweb 的测试,考试内容跟期末考试的差不多,都是实现增删改查,连接多个数据库,实现多个用户的操作。

JDBC.Tools

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Util {

public static Connection getConnection() throws ClassNotFoundException, SQLException{

Connection con = null;
Statement sta = null;
ResultSet res = null;
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sm?useUnicode=true&characterEncoding=utf8", "root", "wmx021207");
return con;
}

public static void close(Connection con) {
try {
if (con != null) {
con.close();
}} catch (SQLException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void close(PreparedStatement pre) {
try {
if (pre != null) {
pre.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet res) {
try {
if (res != null) {
res.close();
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

 DAO

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.NewsBean;
import util.Util;

public class NewsDao {
//添加新闻信息
public int add(NewsBean bean)throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "insert into news1(title,keyword,author,date) values(?,?,?,?)";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.setString(2, bean.getKeyword());
pre.setString(3, bean.getAuthor());
pre.setString(4, bean.getDate());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//删除新闻信息
public int delete(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql="delete from news1 where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//修改新闻信息
public int update(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "update news1 set keyword=?,author=?,date=?, where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.setString(2, bean.getKeyword());
pre.setString(3, bean.getAuthor());
pre.setString(4, bean.getDate());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//查询新闻信息
public int read(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
ResultSet resultSet=null;
try {
String sql = "select * from news1 where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
resultSet = pre.executeQuery();
while (resultSet.next()){
System.out.println("title"+resultSet.getString("title")+"keyword"+resultSet.getString("keyword")+"author"+resultSet.getString("author")+"date"+resultSet.getString("date"));
}
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
Util.close(resultSet);
}
return 1;
}
}

 

BEAN

package bean;

public class NewsBean {

private String title; //标题
private String keyword; //关键字
private String author; //作者
private String date; //日期
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}

 

posted @ 2023-02-13 19:36  傲世小苦瓜  阅读(17)  评论(0)    收藏  举报