码迷,mamicode.com
首页 > 数据库 > 详细

JDBC增删改查简单测试

时间:2016-11-26 20:12:20      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:dstat   tco   except   tin   com   values   test   测试   manager   

首先编写一个entity以便与数据库表文件相对应

lyTable.java

public class LyTable implements java.io.Serializable {
    private Integer id;
    private Integer userId;
    private Date date;
    private String title;
    private String content;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    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;
    }
    @Override
    public String toString() {
        return "LyTable [id=" + id + ", userId=" + userId + ", date=" + date
                + ", title=" + title + ", content=" + content + "]";
    }
    
}

然后编写一个简单的JDBC工具类

public class DBConn {
    private Statement stmt;
    private Connection conn;
    ResultSet rs;
    
    public DBConn(){
        stmt=null;
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=xxx","xx","xx");
        }catch(Exception e){
            e.printStackTrace();
        }
        rs=null;
    }
    /**
     * 获得Connection连接对象
     * @return
     */
    public Connection getConn()
    {
        return this.conn;
    }

}

最后是测试类

public class test {
    DBConn dbconn=new DBConn();
    Connection con=dbconn.getConn();
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    /**
     * search查找
     */
    public void getAll(){
        String sql="select * from TEST..lyTable";
        try {
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()){
                LyTable ly=new LyTable();
                ly.setId(rs.getInt(1));
                ly.setUserId(rs.getInt(2));
                ly.setDate(rs.getDate(3));
                ly.setTitle(rs.getString(4));
                ly.setContent(rs.getString(5));
                System.out.println(ly.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * insert插入
     * @param ly
     */
    private void insert(LyTable ly) {
        String sql = "insert into Test..lyTable values(?,?,?,?)";
        try {
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, ly.getUserId());
            pstmt.setDate(2, ly.getDate());
            pstmt.setString(3,ly.getTitle());
            pstmt.setString(4,ly.getContent());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * delete删除
     * @param id
     */
    private void delete(Integer id) {
        String sql = "delete from TEST..lyTable where id=?";
        try {
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * update 修改
     * @param ly
     */
    private void update(LyTable ly) {
        String sql = "update TEST..lyTable set content=‘"+ly.getContent()+"‘ where title=‘"+ly.getTitle()+"‘";
        try {
            pstmt=con.prepareStatement(sql);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

JDBC增删改查简单测试

标签:dstat   tco   except   tin   com   values   test   测试   manager   

原文地址:http://www.cnblogs.com/circlesmart/p/6104847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!