使用JDBC查询MySQL中用户表的记录
- 数据库初始化
1 /* 2 SQLyog v10.2 3 MySQL - 5.1.72-community : Database - mybatis 4 ********************************************************************* 5 */ 6 7 8 /*!40101 SET NAMES utf8 */; 9 10 /*!40101 SET SQL_MODE=‘‘*/; 11 12 /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 13 /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 14 /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; 15 /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; 16 /*Table structure for table `items` */ 17 18 CREATE TABLE `items` ( 19 `id` int(11) NOT NULL AUTO_INCREMENT, 20 `name` varchar(32) NOT NULL COMMENT ‘商品名称‘, 21 `price` float(10,1) NOT NULL COMMENT ‘商品定价‘, 22 `detail` text COMMENT ‘商品描述‘, 23 `pic` varchar(64) DEFAULT NULL COMMENT ‘商品图片‘, 24 `createtime` datetime NOT NULL COMMENT ‘生产日期‘, 25 PRIMARY KEY (`id`) 26 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 27 28 /*Table structure for table `orderdetail` */ 29 30 CREATE TABLE `orderdetail` ( 31 `id` int(11) NOT NULL AUTO_INCREMENT, 32 `orders_id` int(11) NOT NULL COMMENT ‘订单id‘, 33 `items_id` int(11) NOT NULL COMMENT ‘商品id‘, 34 `items_num` int(11) DEFAULT NULL COMMENT ‘商品购买数量‘, 35 PRIMARY KEY (`id`), 36 KEY `FK_orderdetail_1` (`orders_id`), 37 KEY `FK_orderdetail_2` (`items_id`), 38 CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, 39 CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 40 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 41 42 /*Table structure for table `orders` */ 43 44 CREATE TABLE `orders` ( 45 `id` int(11) NOT NULL AUTO_INCREMENT, 46 `user_id` int(11) NOT NULL COMMENT ‘下单用户id‘, 47 `number` varchar(32) NOT NULL COMMENT ‘订单号‘, 48 `createtime` datetime NOT NULL COMMENT ‘创建订单时间‘, 49 `note` varchar(100) DEFAULT NULL COMMENT ‘备注‘, 50 PRIMARY KEY (`id`), 51 KEY `FK_orders_1` (`user_id`), 52 CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 53 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 54 55 /*Table structure for table `user` */ 56 57 CREATE TABLE `user` ( 58 `id` int(11) NOT NULL AUTO_INCREMENT, 59 `username` varchar(32) NOT NULL COMMENT ‘用户名称‘, 60 `birthday` date DEFAULT NULL COMMENT ‘生日‘, 61 `sex` char(1) DEFAULT NULL COMMENT ‘性别‘, 62 `address` varchar(256) DEFAULT NULL COMMENT ‘地址‘, 63 PRIMARY KEY (`id`) 64 ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; 65 66 /*!40101 SET [email protected]_SQL_MODE */; 67 /*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; 68 /*!40014 SET [email protected]_UNIQUE_CHECKS */; 69 /*!40111 SET [email protected]_SQL_NOTES */;
建表
/* SQLyog v10.2 MySQL - 5.1.72-community : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=‘‘*/; /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; /*Data for the table `items` */ insert into `items`(`id`,`name`,`price`,`detail`,`pic`,`createtime`) values (1,‘台式机‘,3000.0,‘该电脑质量非常好!!!!‘,NULL,‘2015-02-03 13:22:53‘),(2,‘笔记本‘,6000.0,‘笔记本性能好,质量好!!!!!‘,NULL,‘2015-02-09 13:22:57‘),(3,‘背包‘,200.0,‘名牌背包,容量大质量好!!!!‘,NULL,‘2015-02-06 13:23:02‘); /*Data for the table `orderdetail` */ insert into `orderdetail`(`id`,`orders_id`,`items_id`,`items_num`) values (1,3,1,1),(2,3,2,3),(3,4,3,4),(4,4,2,3); /*Data for the table `orders` */ insert into `orders`(`id`,`user_id`,`number`,`createtime`,`note`) values (3,1,‘1000010‘,‘2015-02-04 13:22:35‘,NULL),(4,1,‘1000011‘,‘2015-02-03 13:22:41‘,NULL),(5,10,‘1000012‘,‘2015-02-12 16:13:23‘,NULL); /*Data for the table `user` */ insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (1,‘王五‘,NULL,‘2‘,NULL),(10,‘张三‘,‘2014-07-10‘,‘1‘,‘北京市‘),(16,‘张小明‘,NULL,‘1‘,‘河南郑州‘),(22,‘陈小明‘,NULL,‘1‘,‘河南郑州‘),(24,‘张三丰‘,NULL,‘1‘,‘河南郑州‘),(25,‘陈小明‘,NULL,‘1‘,‘河南郑州‘),(26,‘王五‘,NULL,NULL,NULL); /*!40101 SET [email protected]_SQL_MODE */; /*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; /*!40014 SET [email protected]_UNIQUE_CHECKS */; /*!40111 SET [email protected]_SQL_NOTES */;
数据
1 package cn.itcast.mybatis.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 /** 10 * 11 * <p>Title: JdbcTest</p> 12 * <p>Description:通过单独的jdbc程序,总结其中的问题 </p> 13 * <p>Company: www.itcast.com</p> 14 * @author 传智.燕青 15 * @date 2015-4-22上午9:16:05 16 * @version 1.0 17 */ 18 public class JdbcTest { 19 20 public static void main(String[] args) { 21 22 //数据库连接 23 Connection connection = null; 24 //预编译的Statement,使用预编译的Statement提高数据库性能 25 PreparedStatement preparedStatement = null; 26 //结果 集 27 ResultSet resultSet = null; 28 29 try { 30 //加载数据库驱动 31 Class.forName("com.mysql.jdbc.Driver"); 32 33 //通过驱动管理类获取数据库链接 34 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "mysql"); 35 //定义sql语句 ?表示占位符 36 String sql = "select * from user where username = ?"; 37 //获取预处理statement 38 preparedStatement = connection.prepareStatement(sql); 39 //设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值 40 preparedStatement.setString(1, "王五"); 41 //向数据库发出sql执行查询,查询出结果集 42 resultSet = preparedStatement.executeQuery(); 43 //遍历查询结果集 44 while(resultSet.next()){ 45 System.out.println(resultSet.getString("id")+" "+resultSet.getString("username")); 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 }finally{ 50 //释放资源 51 if(resultSet!=null){ 52 try { 53 resultSet.close(); 54 } catch (SQLException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 if(preparedStatement!=null){ 60 try { 61 preparedStatement.close(); 62 } catch (SQLException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } 66 } 67 if(connection!=null){ 68 try { 69 connection.close(); 70 } catch (SQLException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } 74 } 75 76 } 77 78 79 } 80 }
1、对数据库连接资源的低效率使用
2、SQL比较死
3、结果集映射操作比较重复
时间: 2024-11-03 03:39:22