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