大数据Java基础第二十一天作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据Java基础第二十一天作业相关的知识,希望对你有一定的参考价值。
第一题:使用PreparedStament对mysql的CRUD操作。 CREATE TABLE `edu_user` ( `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘用户ID‘, `user_name` varchar(50) NOT NULL DEFAULT ‘‘ COMMENT ‘用户名‘, `user_age` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘用户年龄‘, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘用户表‘; public 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, password); }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); 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); } } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1794484
以上是关于大数据Java基础第二十一天作业的主要内容,如果未能解决你的问题,请参考以下文章