Big Data Java Foundation 21st day Job

Source: Internet
Author: User

Tag:java   mysql    Big Data    

First question: Use Preparedstament to MySQL for crud operations. create table  ' Edu_user '   (   ' user_id '  int (Ten)  unsigned not null  AUTO_INCREMENT COMMENT  ' User ID ',   ' user_name '  varchar ()  not null  DEFAULT  '  COMMENT  ' username ',   ' user_age '  tinyint (3)  unsigned not  NULL DEFAULT  ' 0 '  COMMENT  ' User age ',  primary key  (' user_id '))   engine=innodb default charset=utf8 comment= ' user table ';p ublic class datademo {     public static void main (String[] args)  throws exception {         db db = new db ();         db. InsertData ();         db. DeleteData ();         db. UpdateData ();          db. Selectdata ();    }}import java.sql.connection;import java.sql.drivermanager; import java.sql.preparedstatement;import java.sql.resultset;public class db {     private connection conn;    public db () {         try {             class.forname ("Com.mysql.jdbc.Driver");             String url =  "Jdbc:mysql://localhost:3306/edu_index";             String username =  "Root";             String password =  "Jiangmin";             this.conn = drivermanager.getconnection ( url, username, p assword);         }catch (exception e) {             e.printstacktrace ();         }    }        public void  InsertData ()  throws exception{        string sql =   "Insert into edu_user (user_id,user_name,user_age)  values (?,?,?)"  ;        PreparedStatement ppst =  Conn.preparestatement (SQL);         ppst.setint (1, 1);         ppst.setstring (2,  "Tomas");         ppst.setint (3, 10);         ppst.execute ();     }        publIc void deletedata ()  throws Exception{         string sql=  "DELETE FROM EDU_USER WHERE USER_ID = ?"  ;        PreparedStatement ppst =  Conn.preparestatement (SQL);         ppst.setint (1, 1);         ppst.execute ();    }         public void updatedata ()  throws Exception{         String sql=  "Update edu_user set user_age = ?,user_name  = ? where user_id = ? "  ;        PreparedStatement ppst =  Conn.preparestatement (SQL);         ppst.setint (1, 100);     &Nbsp;   ppst.setstring (2,  "Aron");         Ppst.setint (3, 1);         ppst.execute ();     }        public void selectdata ()  throws Exception {        string sql=  "Select user_id,user_name,user_age  from edu_user where user_id = ? "  ;        PreparedStatement ppst =  Conn.preparestatement (SQL);         ppst.setint (1, 1);         ppst.execute ();         resultset  rs = ppst.getresultset ();         while (Rs.next ()) {             string usEr_id = rs.getstring ("user_id");             string user_name = rs.getstring ("user_name");             string user_age = rs.getstring ("User_age");             system.out.println ("user_id = "  +  user_id +  ",user_name = "  + user_name +  ",user_age = "   + user_age);         }    }}


This article is from the "Forest Sensitive" blog, please be sure to keep this source http://senlinmin.blog.51cto.com/6400386/1794484

Big Data Java Foundation 21st day Job

Related Article

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.