编写一个日记本程序,用java语言(在intellij-idea内编写),使用MySQL数据库,图形化界面简洁一点。要满足图中要求,代码最好带上注释。
请看:https://blog.csdn.net/weixin_54963748/article/details/119721705
可以自己尝试做一部分,真正遇到不会的知识点再请教,对掌握这些知识有帮助。
看起来不是很难,但比较多小细节,可以自己尝试下,然后有问题,我们可以交流交流
一个简单的记事本,要实现添加和查询的功能。(添加到数据库中,并且从数据库中查询)。
本代码是把数据存到数据库中,需要有一点点数据库(增,查)的知识,还需要会一些JDBC(就是java连接数据库)的知识。不会的可以先去学习一下JDBC,内容不是很多。
import java.sql.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ConnectionTest02 extends JFrame{
public static void main(String[] args){
new ConnectionTest02().init();
}
JFrame f = new JFrame("记事本");
JFrame f_1 = new JFrame("查询");
JLabel label01 = new JLabel("日期: ");
JLabel label02 = new JLabel("内容: ");
JLabel label03 = new JLabel("图片路径: ");
JLabel label04 = new JLabel("请输入要查询的time:");
JLabel label05 = new JLabel();
JLabel label06 = new JLabel("查询内容: ");
JTextField textfield = new JTextField(25);
JTextArea textArea = new JTextArea(10, 30);
JTextArea textArea02 = new JTextArea(10, 30);
JTextField textfield02 = new JTextField(20);
JTextField textfield03 = new JTextField(20);
JButton button01 = new JButton("确定保存");
JButton button02 = new JButton("清空");
JButton button03 = new JButton("查询");
JButton button04 = new JButton("查询");
public void init(){
JPanel p = new JPanel();
p.add(label01);
p.add(textfield);
JPanel p02 =new JPanel();
p02.add(label02);
p.add(button01);
p02.add(textArea);
JPanel p03 =new JPanel();
p03.add(label03);
p03.add(textfield02);
JPanel p04 =new JPanel();
p03.add(button02);
p03.add(button03);
// 这里使用的是Box垂直组件
Box vertical = Box.createVerticalBox();
vertical.add(p);
vertical.add(p02);
vertical.add(p03);
vertical.add(p04);
f.add(vertical);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 这是JFrame中window包中的方法,目的是使得页面显示在中间位置
f.setLocationRelativeTo(null);
// 按钮-保存-处理
button01.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
testConnextion3();
clear();
}
});
// 按钮-清空-处理
button02.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
clear();
}
});
// 按钮-查询-处理
// 进行查询的时候将会产生一个新的页面
button03.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
init2();
}
});
// 设置宽高
f.setSize(500,500);
f.setVisible(true);
}
public void init2(){
JPanel p05 =new JPanel();
p05.add(label04);
p05.add(textfield03);
p05.add(button04);
p05.add(label05);
JPanel p06 = new JPanel();
p06.add(label06);
p06.add(textArea02);
// // 这里使用的是Box垂直组件
Box vertical02 = Box.createVerticalBox();
vertical02.add(p05);
vertical02.add(p06);
f_1.add(vertical02);
f_1.pack();
f_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 这是JFrame中window包中的方法,目的是使得页面显示在中间位置
f_1.setLocationRelativeTo(null);
// 按钮查询处理
button04.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
look();
}
});
// 设置宽高
f_1.setSize(500,500);
f_1.setVisible(true);
}
// 最终版--连接(将连接需要的信息保存在配置文件中)
public void testConnextion3(){
Connection conn = null;
PreparedStatement ps = null;
String str01 = textfield.getText();
String str02 = textArea.getText();
String str03 = textfield02.getText();
try{
// 使用资源绑定器绑定属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
// 1、注册驱动
Class.forName(driver);
// 2、获取连接
conn = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象 =" + conn);
// 这里的图片路径我只是把图片和java文件放在了同一个文件夹下。所以保存的时候我只保存的是图片的文件名
String sql = "insert into riji(id,text,bir,url)values(?,?,?,?)";
ps = conn.prepareStatement(sql); // 用于将参数化的SQL语句发送到数据库。
// 填充占位符
ps.setString(1,str01);
ps.setString(2,str02);
ps.setString(3,"2021-6-8");
ps.setString(4,str03);
// 执行
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
// 资源关闭
try{
if(ps != null)
ps.close();
}catch(SQLException e){
e.printStackTrace();
}
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
// 按钮清空方法
public void clear(){
textfield.setText("");
textArea.setText("");
textfield02.setText("");
}
// 查找功能的实现
public void look(){
String str04 = textfield03.getText();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
// 使用资源绑定器绑定属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
// 1、注册驱动
Class.forName(driver);
// 2、获取连接
conn = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象 =" + conn);
// 预编译sql语句
String sql = "select id,text,bir,url from riji where id = ?";
ps = conn.prepareStatement(sql); // 用于将参数化的SQL语句发送到数据库。
ps.setString(1,str04);
rs = ps.executeQuery();
if(rs.next()){
String id =rs.getString(1);
String text = rs.getString(2);
String bir = rs.getString(3);
String url01 = rs.getString(4);
System.out.println(id +" , " + text+" , " + bir + "图片路径:"+url01);
textArea02.append(text); // 查询的时候把文字显示出来
label05.setPreferredSize(new Dimension(100,100)); // 设置label的大小,就是可以固定图片输出的大小
label05.setIcon(new ImageIcon(url01)); // 把对应的图片显示出来
}
ps.execute();
}catch (Exception e){
e.printStackTrace();
}finally{
// 资源关闭
try{
if(ps != null)
ps.close();
}catch(SQLException e){
e.printStackTrace();
}
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
try{
if(rs!=null)
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.util.Scanner;
public class IO_日记本 {
/**
* 模拟日记本程序
*/
private static String filePath;
private static String message = "";
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("---------日记本---------");
System.out.println("1.写日记");
System.out.println("2.查看日记");
System.out.println("3.修改日记");
System.out.println("4.保存");
System.out.println("5.退出");
System.out.println("注意:每次输入内容后记得保存!");
System.out.print("请输入操作指令:");
int command = sc.nextInt();
switch (command) {
case 1:
// 1:新建文件(写日记)
createFile();
break;
case 2:
// 2:打开文件(查看日记)
openFile();
break;
case 3:
// 3:修改文件
editFile();
break;
case 4:
// 4:保存
saveFile();
break;
case 5:
// 5:退出
System.out.println("谢谢使用本系统,欢迎下次再来!");
System.exit(0);
break;
default:
System.out.println("您输入的指令错误!");
break;
}
}
}
//写一个方法写入文件内容
private static void createFile() {
message = "";//新建文件时,暂存文件内容清空
Scanner sc = new Scanner(System.in);
System.out.println("请输入内容,停止编写请输入:stop");
StringBuffer stb = new StringBuffer();//用于后期输入内容的拼接
String inputMessage = "";
while (!inputMessage.equals("stop")) {//输入stop则停止
if (stb.length() > 0) {
stb.append("\r\n");//追加换行符
}
stb.append(inputMessage);//拼接输入信息
inputMessage = sc.nextLine();//获取输入信息
}
message = stb.toString();//将输入内容缓存
}
//写一个方法保存文件
private static void saveFile() throws Exception {
FileWriter out = new FileWriter("文件路径", true);
out.write(message + "\r\n");//将输入内容写入
message = "";
out.close();
}
//写一个方法打开文件
public static void openFile() throws Exception {
Reader r = new FileReader("文件路径");
BufferedReader br = new BufferedReader(r);
// char[] c = new char[1024];//定义一个桶装每次读取多少个数据
// int len;
// while ((len = br.read(c)) != -1) {//读取所有内容,如果读完所有内容则停止
// String str = new String(c, 0, len);//每次读取0到len的所有内容
// System.out.print(str);//因为读取时会自动换行所以这里我们不需要换行
// }
String line;
while ((line = br.readLine()) != null) {//一次读取一行
System.out.println(line);//因为读取一行时程序不会自己换行,所以这里我们需要给它换行
}
//System.out.println();//读完换行
}
//写一个方法修改文件
/**
* 替换
* @throws IOException
*/
public static void editFile() throws IOException{
Scanner sc = new Scanner(System.in);
//原有的内容
System.out.println("原文件内容:");
String str1 =sc.next();;
//要替换的内容
System.out.println("修改成:");
String str2 =sc.next();
// 读取
File file = new File("文件路径");
FileReader in = new FileReader(file);
BufferedReader buf = new BufferedReader(in);//缓冲流
// 内存流, 作为临时流
CharArrayWriter tempStream = new CharArrayWriter();
// 替换
String line = null;
while ( (line = buf.readLine()) != null) {
// 替换每行中, 符合条件的字符串
line = line.replaceAll(str1, str2);//正则表达式
// 将该行写入内存
tempStream.write(line+"\r\n");
}
// 关闭输入流
buf.close();
// 将内存中的流写入文件
FileWriter fw = new FileWriter(file);
tempStream.writeTo(fw);
fw.close();
}
}
import java.io.*;
import java.util.Scanner;
public class IO_日记本 {
/**
* 模拟日记本程序
*/
private static String filePath;
private static String message = "";
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("---------日记本---------");
System.out.println("1.写日记");
System.out.println("2.查看日记");
System.out.println("3.修改日记");
System.out.println("4.保存");
System.out.println("5.退出");
System.out.println("注意:每次输入内容后记得保存!");
System.out.print("请输入操作指令:");
int command = sc.nextInt();
switch (command) {
case 1:
// 1:新建文件(写日记)
createFile();
break;
case 2:
// 2:打开文件(查看日记)
openFile();
break;
case 3:
// 3:修改文件
editFile();
break;
case 4:
// 4:保存
saveFile();
break;
case 5:
// 5:退出
System.out.println("谢谢使用本系统,欢迎下次再来!");
System.exit(0);
break;
default:
System.out.println("您输入的指令错误!");
break;
}
}
}
//写一个方法写入文件内容
private static void createFile() {
message = "";//新建文件时,暂存文件内容清空
Scanner sc = new Scanner(System.in);
System.out.println("请输入内容,停止编写请输入:stop");
StringBuffer stb = new StringBuffer();//用于后期输入内容的拼接
String inputMessage = "";
while (!inputMessage.equals("stop")) {//输入stop则停止
if (stb.length() > 0) {
stb.append("\r\n");//追加换行符
}
stb.append(inputMessage);//拼接输入信息
inputMessage = sc.nextLine();//获取输入信息
}
message = stb.toString();//将输入内容缓存
}
//写一个方法保存文件
private static void saveFile() throws Exception {
FileWriter out = new FileWriter("文件路径", true);
out.write(message + "\r\n");//将输入内容写入
message = "";
out.close();
}
//写一个方法打开文件
public static void openFile() throws Exception {
Reader r = new FileReader("文件路径");
BufferedReader br = new BufferedReader(r);
// char[] c = new char[1024];//定义一个桶装每次读取多少个数据
// int len;
// while ((len = br.read(c)) != -1) {//读取所有内容,如果读完所有内容则停止
// String str = new String(c, 0, len);//每次读取0到len的所有内容
// System.out.print(str);//因为读取时会自动换行所以这里我们不需要换行
// }
String line;
while ((line = br.readLine()) != null) {//一次读取一行
System.out.println(line);//因为读取一行时程序不会自己换行,所以这里我们需要给它换行
}
//System.out.println();//读完换行
}
//写一个方法修改文件
/**
* 替换
* @throws IOException
*/
public static void editFile() throws IOException{
Scanner sc = new Scanner(System.in);
//原有的内容
System.out.println("原文件内容:");
String str1 =sc.next();;
//要替换的内容
System.out.println("修改成:");
String str2 =sc.next();
// 读取
File file = new File("文件路径");
FileReader in = new FileReader(file);
BufferedReader buf = new BufferedReader(in);//缓冲流
// 内存流, 作为临时流
CharArrayWriter tempStream = new CharArrayWriter();
// 替换
String line = null;
while ( (line = buf.readLine()) != null) {
// 替换每行中, 符合条件的字符串
line = line.replaceAll(str1, str2);//正则表达式
// 将该行写入内存
tempStream.write(line+"\r\n");
}
// 关闭输入流
buf.close();
// 将内存中的流写入文件
FileWriter fw = new FileWriter(file);
tempStream.writeTo(fw);
fw.close();
}
}
http://t.csdn.cn/fBBeK
这有个符号你的需求。
参考 https://blog.csdn.net/weixin_54963748/article/details/119721705
参考 https://blog.csdn.net/weixin_54963748/article/details/119721705