1 packagecom.day01.station.dao;2
3 /**
4 * Created by Administrator on 2018/2/1.5 */
6
7 importjava.sql.Connection;8 importjava.sql.DriverManager;9 importjava.sql.ResultSet;10 importjava.sql.Statement;11
12 /**
13 * jdbc 连接数据库 加 连 语 执 释14 * 1.加载15 * 2.连接16 * 3.创建编译语句17 * 4.执行语句18 * 5.释放资源19 */
20 public classProductDao {21 //增加 一个产品 包括 名称 和 卖价
22 public void save(String productName, intsalePrice) {23 System.out.println("--------我是增加方法---------");24 System.out.println(" productName = " + productName + " , salePrice =" +salePrice);25
26 try{27 //1. 加载
28 Class.forName("com.mysql.jdbc.Driver");29 //* 2. 连接30 //int age =1831 //static Connection getConnection(String url, String user, String password)32 //试图建立到给定数据库 URL 的连接。
33 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");34 //* 3. 创建编译语句35 //Statement createStatement()36 //创建一个 Statement 对象来将 SQL 语句发送到数据库。
37 Statement statement =connection.createStatement();38 //* 4. 执行语句39 //int executeUpdate(String sql)40 //执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
41 String sql="INSERT INTO product (product_name,sale_price) VALUES ('小米手机',1010)";42 statement.executeUpdate(sql);43 //* 5. 释放资源
44 statement.close();45 connection.close();46
47 } catch(Exception e) {48 e.printStackTrace();49 }50
51
52 }53
54 //删除 根据id删除产品
55 public void delete(intid) {56 System.out.println(" --------我是删除方法--------- ");57 System.out.println("---id=" +id);58
59 try{60 //1.加载
61 Class.forName("com.mysql.jdbc.Driver");62 //2.连接
63 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");64 //3.创建编译语句
65 Statement statement =connection.createStatement();66 //4.执行语句
67 String sql="DELETE FROM product WHERE id=31";68 statement.executeUpdate(sql);69 //5.释放资源
70 statement.close();71 connection.close();72
73 } catch(Exception e) {74 e.printStackTrace();75 }76
77 }78
79 //修改 根据id 修改产品的名称
80 public void update(intid, String productName) {81 System.out.println("--------我是修改方法---------");82 System.out.println(" productName = " + productName + " , id =" +id);83 try{84 //1.加载
85 Class.forName("com.mysql.jdbc.Driver");86 //2.连接
87 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");88 //3.创建编译语句
89 Statement statement =connection.createStatement();90 //4.执行语句
91 String sql="UPDATE product SET sale_price=500,cost_price=200 WHERE id= 26 ";92 statement.executeUpdate(sql);93 //5.释放资源
94 statement.close();95 connection.close();96
97 } catch(Exception e) {98 e.printStackTrace();99 }100
101
102 }103
104 //查询
105 publicString query() {106 System.out.println("------我是查询方法----------");107 try{108 //1.加载
109 Class.forName("com.mysql.jdbc.Driver");110 //2.连接
111 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");112 //3.创建编译语句
113 Statement statement =connection.createStatement();114 //4.执行语句115 //ResultSet executeQuery(String sql)116 //执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。
117 String sql = "SELECT id,product_name,sale_price FROM product WHERE id=9";118 ResultSet resultSet =statement.executeQuery(sql);119 //解析结果
120 while (resultSet.next()){//如果有在执行里面121 //int getInt(int columnIndex)122 //以 Java 编程语言中 int 的形式获取此 ResultSet 对象的当前行中指定列的值。
123 int id = resultSet.getInt("id");124 System.out.println(" id = "+id);125 //String getString(String columnLabel)126 //以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。
127 String productName = resultSet.getString("product_name");128 System.out.println(" productName ="+productName);129 //获取卖价
130 int salePrice = resultSet.getInt("sale_price");131 System.out.println(" salePrice = "+salePrice);132 }133 //5.释放资源
134 resultSet.close();135 statement.close();136 connection.close();137
138 } catch(Exception e) {139 e.printStackTrace();140 }141 return "苹果手机";142 }143
144 }