- 实现界面
2.项目结构
3. 数据库
4. 实现代码
1)接口
package memorywords.dao;
public interface MemoryWords {
/*
* 顺便复习下接口相关的知识:
* 1. 接口中的方法全部是public抽象方法(就算不写abstract,public,它也是public抽象方法)
* 2. 接口中的变量只可以用static final 来修饰
* 3. 接口不可以有构造方法,因为接口不可以被实例化,也就是不可以创建对象
* 4. 接口可以继承其他接口
* 5. 一个类可以实现多个接口
*/
//1. 录入单词
void AddWords(String ch,String jp);
//2. 修改单词
void ReviseWords(String id,String jp);
//3. 删除单词
void DeleteWords(String ch );
//4. 记忆单词,随机出现中文单词,要写出对应的日语,
void RemenberWords();
//5. 查询所有
String FindAll();
}
2)实现方法
package memorywords.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import memorywords.util.JDBC_util;
import memorywords.dao.MemoryWords;
public class MemoryWordsImplement implements MemoryWords {
//实现添加方法
@Override
public void AddWords(String ch, String jp) {
Connection conn = null;
PreparedStatement ps = null;
try {
String sql="insert into JapaneseDate values(null,?,?,0,0,0)";
conn = JDBC_util.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, ch);
ps.setString(2, jp);
int rs = ps.executeUpdate();
if(rs>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_util.release(ps, conn);
}
}
// 实现修改方法
@Override
public void ReviseWords(String id, String jp) {
Connection conn = null;
PreparedStatement ps = null;
try {
String sql="update JapaneseDate set Japanese=? where id=?";
conn = JDBC_util.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, jp);
ps.setString(2, id);
int rs = ps.executeUpdate();
if(rs>0){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_util.release(ps, conn);
}
}
// 实现删除方法
@Override
public void DeleteWords(String ch) {
Connection conn = null;
PreparedStatement ps = null;
try {
String sql="delete from JapaneseDate where Chinese=?";
conn = JDBC_util.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, ch);
int rs = ps.executeUpdate();
if(rs>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_util.release(ps, conn);
}
}
private Boolean toContinue(Scanner sc){
System.out.println("请选择:1.退出请输入‘1’;2.继续请按任意键");
String opt = sc.nextLine().trim();
Boolean flag = true;
if(opt.equals("1")){
flag = false;
return flag;
}else{
return flag;
}
}
// 实现背单词方法
@Override
public void RemenberWords () {
Connection conn = null;
PreparedStatement ps = null;
Statement st = null;
ResultSet rs = null;
ResultSet rs0 = null;
Scanner sc = new Scanner(System.in);
ArrayList<String> idArray = new ArrayList<String>();
Random rdid = new Random();
Boolean flag = null;
try {
conn = JDBC_util.getConn();
String sql0 = "select id from JapaneseDate";
st = conn.createStatement();
rs0 = st.executeQuery(sql0);
while(rs0.next()){
String no = rs0.getString("id");
idArray.add(no);
}
while(true){
int idx = rdid.nextInt(idArray.size());
String id = idArray.get(idx);
String sql = "select * from JapaneseDate where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
if(rs.next())