JDBC向數據庫中插入數據


新建數據庫,並插入相關數據。

 1 create database bbs;
 2 
 3 use bbs;
 4 
 5 create table article 
 6 (
 7 id int primary key auto_increment,
 8 pid int,
 9 rootid int,
10 title varchar(255),
11 cont text,
12 pdate datetime,
13 isleaf int 
14 );
15 
16 insert into article values (null, 0, 1, '螞蟻大戰大象', '螞蟻大戰大象', now(), 1);
17 insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
18 insert into article values (null, 2, 1, '螞蟻也不好過','螞蟻也不好過', now(), 0);
19 insert into article values (null, 2, 1, '瞎說', '瞎說', now(), 1);
20 insert into article values (null, 4, 1, '沒有瞎說', '沒有瞎說', now(), 0);
21 insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
22 insert into article values (null, 6, 1, '怎么沒有可能', '怎么沒有可能', now(), 0);
23 insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
24 insert into article values (null, 2, 1, '大象進醫院了', '大象進醫院了', now(), 1);
25 insert into article values (null, 9, 1, '護士是螞蟻', '護士是螞蟻', now(), 0);

 為每一張表建立一個實體類,實體類中的屬性對應着表的字段名。

 1 package qddx.JDBC;
 2 import java.sql.*;
 3 public class bbsVo {
 4 
 5     private int id;
 6     private int pid;
 7     private int rootid;
 8     private String title;
 9     private String cont;
10     private Timestamp pdate;
11     private int isleaf;
12     
13     public int getId(){
14         return id;
15     }
16     public void setId(int id){
17         this.id = id;
18     }
19     
20     public int getPid(){
21         return pid;
22     }
23     public void setPid(int pid){
24         this.pid = pid;
25     }
26     
27     public int getRootid(){
28         return rootid;
29     }
30     public void setRootid(int rootid){
31         this.rootid = rootid;
32     }
33     
34     public String getTitle(){
35         return cont;
36     }
37     public void setCont(String title){
38         this.title = title;
39     }
40     
41     public String getCont(){
42         return title;
43     }
44     public void setTitle(String cont){
45         this.cont = cont;
46     }
47     public Timestamp getPdate(){
48         return pdate;
49     }
50     public void setPdate(Timestamp pdate){
51         this.pdate = pdate;
52     }
53     
54     public int getIsleaf(){
55         return isleaf;
56     }
57     public void setIsleaf(int isleaf){
58         this.isleaf = isleaf;
59     }
60     
61 }

插入數據

package qddx.JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.sql.*;
public class Addbbs {

    public void add(bbsVo vo){
        Connection conn = null;
         PreparedStatement pst = null;
         ResultSet rs = null;
         try{
         conn = JDBC_Connection.getConnection();
         String sql = "INSERT INTO ARTICLE(id,pid,rootid,title,cont,pdate,isleaf) values(?,?,?,?,?,?,?)";
         pst=conn.prepareStatement(sql);
         //把相應的參數 添加到pst對象中
         pst.setInt( 1, vo.getId());
         pst.setInt(2, vo.getPid());
         pst.setInt(3, vo.getRootid());
         pst.setString(4, vo.getTitle());
         pst.setString(5, vo.getCont());
         pst.setTimestamp(6, vo.getPdate());
         pst.setInt(7, vo.getIsleaf());
         //提交pst對象
         pst.executeUpdate();
         }catch(SQLException e){
             e.printStackTrace();
         }finally{
             JDBC_Connection.free(rs, conn, pst);//關閉數據庫連接
         }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Addbbs addbbs = new Addbbs();
        bbsVo vo = new bbsVo();
        int id=13;
        int pid = 10;
        int rootid = 1;
        String title = "螞蟻";
        String cont = "螞蟻";
        java.util.Date date = new java.util.Date();
        Timestamp pdate = new Timestamp(date.getTime());
        int isleaf = 1;
        //設置要添加的變量值,放入bbsvo中
        vo.setId(id);
        vo.setPid(pid);
        vo.setRootid(rootid);
        vo.setTitle(cont);
        vo.setCont(title);
        vo.setIsleaf(isleaf);
        vo.setPdate(pdate);
        addbbs.add(vo);
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM