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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

天飞 2016-05-16 15:08:30 浏览1112
展开阅读全文

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

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

Message.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;
    }
}
    
复制代码

 

MessageDAO.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;
        }
    }
复制代码

 

MessageDAODemo.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));
            }
        }
    }
}
复制代码

 

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

网友评论

登录后评论
0/500
评论
天飞
+ 关注