JDBC inserts data into the database

Source: Internet
Author: User

Create a new database and insert related data.

1 CREATE database BBS;2 3 Use BBS;4 5 CREATE TABLE Article6 (7Idintprimary Key auto_increment,8Pidint,9Rootidint,TenTitle varchar (255), One cont text, A pdate datetime, -IsLeafint  - ); the  -INSERT into article values (NULL, 0, 1, ' Ant vs Elephant ', ' Ant vs Elephant ', Now (), 1); -INSERT into article values (NULL, 1, 1, ' The elephant was beaten down ', ' The elephant was beaten down ', now (), 1); -INSERT into article values (NULL, 2, 1, ' ants are not good ', ' ants are not good ', now (), 0); +INSERT into article values (NULL, 2, 1, ' nonsense ', ' nonsense ', now (), 1); -INSERT into article values (NULL, 4, 1, ' no nonsense ', ' no nonsense ', now (), 0); +INSERT into article values (NULL, 1, 1, ' How could ', ' how could ', now (), 1); AINSERT into article values (NULL, 6, 1, ' How no possibility ', ' How not possible ', now (), 0); atINSERT into article values (NULL, 6, 1, ' possibility is great ', ' probability is great ', now (), 0); -INSERT into article values (NULL, 2, 1, ' Elephant in hospital ', ' elephant in hospital ', now (), 1); -INSERT into article values (NULL, 9, 1, ' Nurse is ant ', ' nurse is Ant ', now (), 0);

Create an entity class for each table, and the attributes in the entity class correspond to the field names of the table.

1  PackageQDDX. JDBC;2 ImportJava.sql.*;3  Public classBBSVO {4 5     Private intID;6     Private intpid;7     Private intRootid;8     PrivateString title;9     PrivateString cont;Ten     PrivateTimestamp pdate; One     Private intisleaf; A      -      Public intgetId () { -         returnID; the     } -      Public voidSetId (intID) { -          This. ID =ID; -     } +      -      Public intGetpid () { +         returnpid; A     } at      Public voidSetpid (intpid) { -          This. PID =pid; -     } -      -      Public intGetrootid () { -         returnRootid; in     } -      Public voidSetrootid (intRootid) { to          This. Rootid =Rootid; +     } -      the      PublicString GetTitle () { *         returncont; $     }Panax Notoginseng      Public voidSetcont (String title) { -          This. title =title; the     } +      A      PublicString Getcont () { the         returntitle; +     } -      Public voidsettitle (String cont) { $          This. Cont =cont; $     } -      PublicTimestamp getpdate () { -         returnpdate; the     } -      Public voidsetpdate (Timestamp pdate) {Wuyi          This. pdate =pdate; the     } -      Wu      Public intgetisleaf () { -         returnisleaf; About     } $      Public voidSetisleaf (intisleaf) { -          This. IsLeaf =isleaf; -     } -      A}

Inserting data

 PackageQDDX. JDBC;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.Timestamp;ImportJava.sql.*; Public classAddbbs { Public voidAdd (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); //add the corresponding parameters to the PST objectPst.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 ()); //submit a PST objectpst.executeupdate (); }Catch(SQLException e) {e.printstacktrace (); }finally{Jdbc_connection.free (RS, conn, PST);//To close a database connection         }    }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubAddbbs Addbbs =NewAddbbs (); Bbsvo Vo=NewBBSVO (); intId=13; intPID = 10; intRootid = 1; String title= "Ant"; String Cont= "Ant"; Java.util.Date Date=Newjava.util.Date (); Timestamp pdate=NewTimestamp (Date.gettime ()); intIsLeaf = 1; //set the value of the variable to be added and put it in BBSVOVo.setid (ID);        Vo.setpid (PID);        Vo.setrootid (Rootid);        Vo.settitle (cont);        Vo.setcont (title);        Vo.setisleaf (IsLeaf);        Vo.setpdate (pdate);    Addbbs.add (VO); }}

JDBC inserts data into the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.