第11章Java数据库编程
一、选择题
1. A2.B3.B4.C5.C 6.A
二、简答题
1.JDBC的全称是Java DataBase Connection,也就是Java数据库连接,我们可以用它来操作关系型数据库。JDBC接口及相关类在java.sql包和javax.sql包里。我们可以用它来连接数据库,执行SQL查询,存储过程,并处理返回的结果。
2.在查询数据库后会返回一个ResultSet,它就像是查询结果集的一张数据表。
ResultSet对象维护了一个游标,指向当前的数据行。开始的时候这个游标指向的是第一行。如果调用了ResultSet的next()方法游标会下移一行,如果没有更多的数据了,next()方法会返回false。可以在for循环中用它来遍历数据集。
3.Statement和PreparedStatement都是用于执行SQL语句的句柄,但是PreparedStatement代表的是一个预编译的SQL。这两种对象的区别主要体现在以下3个方面:
(1)使用方面的区别:statement执行的SQL语句必须是一个完整的SQL,而对于PreparedStatement来说,可以使用“?”作为SQL语句当中的占位符,然后使用PreparedStatement的setXXX方法来给占位符赋值,最后再执行;
(2)使用Statement时,如果SQL当中出现了“‘”或者“-”等符号时,需要使用转义字符来进行转义,而在PreparedStatement当中,如果占位符的值当中有这些符号,PreparedStatement会自动的进行转义;
(3)PreparedStatement会将SQL语句进行预编译,每次执行的时候只需要将参数设置给相应的占位符就可以运行。而使用Statement时,SQL语句每次都要进行编译,所以PreparedStatement的效率相对较高。
三、程序填空题
1.Class.forName("com.mysql.cj.jdbc.Driver");
2.DriverManager.getConnection(url,username,password);
3.conn.close();
4.ps.executeUpdate();
5.
(1)"select * from worker";
(2)ps.executeQuery();
(3)rs.getString(2)
(4)rs.getString(3)
(5)rs.getString(4)
四、编程题
com.news.util包下
//BaseConnection.java
package com.news.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class BaseConnection {
public static Connection getConnection(){//用这个方法获取mysql的连接
Connection conn=null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动类
conn=DriverManager. getConnection("jdbc:mysql://localhost:3306/newsmanager?useSSL=false&serverTimezone=Asia/Shanghai","root","111111");//(url数据库的IP地址,user数据库用户名,password数据库密码)
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void main(String[] args){//测试数据库是否连接成功的方法
Connection conn=BaseConnection.getConnection();
System.out.println(conn);
}
}
com.news.bean包下
//News.java
package com.news.bean;
public class News {
private int id;
private String title;
private String content;
private int type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return "《"+title+"》";
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", content=" + content
+ ", type=" + type + "]";
}
}
//NewsType.java
package com.news.bean;
public class NewsType {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "NewsType [id=" + id + ", name=" + name + "]";
}
}
com.news.dao包下
//NewsDAO.java
package com.news.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.news.bean.News;
import com.news.util.BaseConnection;
public class NewsDAO {
public ArrayList getList() {// 单表查询
ArrayList ar = new ArrayList();// 存储从数据库中取出来的数据
Connection conn = BaseConnection.getConnection();// 获取数据库连接
// sql执行器对象
PreparedStatement ps = null;
// 结果集对象
ResultSet rs = null;// 查询出来的数据先放到rs中
try {
String sql = "select * from news";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();// 执行数据库查询的方法,放到rs中
while (rs.next()) {// rs对象相当于一个指针,指向数据库的一横行数据
News ne = new News();// 封装数据
ne.setId(rs.getInt("id"));// rs指针指向id一行获取id一行数据,存储到ne中
ne.setTitle(rs.getString("title"));// rs指针指向title一行获取id一行数据,存储到ne中
ne.setContent(rs.getString("content"));// rs指针指向content一行获取id一行数据,存储到ne中
ne.setType(rs.getInt("type"));// rs指针指向id一行获取type一行数据,存储到ne中
ar.add(ne);// 通过循环,把数据的数据依次存储在ne对象中,再把ne对象添加到ar数组中方便提取
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 重点下面代码必须写,当数据库使用后必须关闭,如果没有关闭数据库的接口有限,下次就不能连接
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return ar;// 返回ar
}
// 该方法负责将传递过来的news对象中的数据存入到数据库中
public void insert(News ne) {
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "insert into news(title,content,type)" + "values(' " + ne.getTitle() + "','" + ne.getContent()
+ "'," + ne.getType() + ")";
try {
ps = conn.prepareStatement(sql);// 把写好的sql语句传递到数据库,让数据库知道我们要干什么
int a = ps.executeUpdate();// 这个方法用于改变数据库数据,a代表改变数据库的条数
if (a > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
public void delete(int id) {// 删除数据库中的数据
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "delete from news where id =?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
int a = ps.executeUpdate();
if (a > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
// 本方法用于将传递过来的news对象中的值,根据id主键,改变数据库中的值
public void update(News ne) {
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "update news set title=?,content=?,type=?" + "where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ne.getTitle());
ps.setString(2, ne.getContent());
ps.setInt(3, ne.getType());
ps.setInt(4, ne.getId());
int a = ps.executeUpdate();
if (a > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
com.news.main包下
//TestMain.java
package com.news.main;
import java.util.ArrayList;
import java.util.Scanner;
import com.news.bean.News;
import com.news.dao.NewsDAO;
public class TestMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
NewsDAO nd = new NewsDAO();
while (true) {
System.out.println("1.查看新闻2.添加新闻3.删除新闻4.退出");
int a = sc.nextInt();
if (a == 1) {
ArrayList ar = nd.getList();
System.out.println("编号\t标题\t内容");
for (News ne : ar) {
System.out.println(ne.getId() + "\t" + ne.getTitle()
+ "\t" + ne.getContent());
}
} else if (a == 2) {
System.out.println("请输入新闻标题内容类别编号");
News ne = new News();
ne.setTitle(sc.next());
ne.setContent(sc.next());
ne.setType(sc.nextInt());
nd.insert(ne);// 调用插入方法
} else if (a == 3) {
System.out.println("请输入要删除的新闻编号");
int id = sc.nextInt();
nd.delete(id);// 调用删除方法
} else {
break;
}
}
}
}
×
确定