JAVA数据库连接的另一种实现及简单的数据插入及显示

教材是JDK8的,家里也可以正规的测试JDK8,

但公司电脑是JDK6的,所以代码要相应的变动一下下,以适应老的TRY语句。

Message.java

JAVA数据库连接的另一种实现及简单的数据插入及显示
package cc.openhome;

import java.io.Serializable;

public class Message implements Serializable {
    private Long id;
    private String name;
    private String email;
    private String msg;
    
    public Message() {}
    public Message(String name, String email, String msg) {
        this.name = name;
        this.email = email;
        this.msg = msg;
    }
    
    public String getEmail(){
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
    
JAVA数据库连接的另一种实现及简单的数据插入及显示

 

MessageDAO.java

JAVA数据库连接的另一种实现及简单的数据插入及显示
package cc.openhome;

import java.sql.*;
import java.util.*;

public class MessageDAO {
    private String url;
    private String user;
    private String passwd;

    public MessageDAO(String url, String user, String passwd) {
        this.url = url;
        this.user = user;
        this.passwd = passwd;
    }

    public void add(Message message) {
        try {
            Connection conn = DriverManager.getConnection(url, user, passwd);
            Statement statement = conn.createStatement();
            statement.executeUpdate(
                "INSERT INTO t_message(name, email, msg) VALUES('"
                + message.getName() + "', '"
                + message.getEmail() + "', '"
                + message.getMsg() + "')");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

        public List<Message> get() {
            List<Message> messages = null;
            try {
                Connection conn = DriverManager.getConnection(url, user, passwd);
                Statement statement = conn.createStatement();
                ResultSet result = statement.executeQuery(
                    "SELECT * FROM t_message");
                    messages = new ArrayList<Message>();
                    while(result.next()) {
                        Message message = new Message();
                        message.setId(result.getLong(1));
                        message.setName(result.getString(2));
                        message.setEmail(result.getString(3));
                        message.setMsg(result.getString(4));
                        messages.add(message);
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            return messages;
        }
    }
JAVA数据库连接的另一种实现及简单的数据插入及显示

 

MessageDAODemo.java

JAVA数据库连接的另一种实现及简单的数据插入及显示
package cc.openhome;

import java.util.Scanner;

public class MessageDAODemo {
    public static void main(String args[]) throws Exception {
        Scanner scanner = new Scanner(System.in);
        MessageDAO dao = new MessageDAO(
            "jdbc:mysql://1.2.3.4:3306/demo?" +
            "useUnicode=true&characterEncoding=UTF8",
            "user", "pwd");
        while (true) {
            System.out.print("(1) Show Message (2) Add Message: ");
            switch(Integer.parseInt(scanner.nextLine())) {
                case 1:
                    for (Message message : dao.get()) {
                        System.out.printf("%d\t%s\t%s\t%s%n",
                            message.getId(),
                            message.getName(),
                            message.getEmail(),
                            message.getMsg());
                        }
                        break;
                case 2:
                System.out.print("Name: ");
                String name = scanner.nextLine();
                System.out.print("Email: ");
                String email = scanner.nextLine();
                System.out.print("Message: ");
                String msg = scanner.nextLine();
                dao.add(new Message(name, email, msg));
            }
        }
    }
}
JAVA数据库连接的另一种实现及简单的数据插入及显示

 

注意编译命令的先后顺序(何时带-d. 何时带-cp . 何时需要引用MYSQL的jdbc驱动和自己的类;)

JAVA数据库连接的另一种实现及简单的数据插入及显示

JAVA数据库连接的另一种实现及简单的数据插入及显示

上一篇:java代码调用使用cxf搭建的webService服务传递对象


下一篇:Apache Server 2.2 多站点域名转向配置文件