1.原生态JDBC编程中的问题总结

### **1.1 环境**

Java环境:jdk1.7.0\_72

Eclipse:indigo

Mysql:5.1

### **1.2 创建mysql数据库**

导入下面的脚本:

![](data:image/*;base64,iVBORw0KGgoAAAANSUhEUgAAAIoAAAA+CAIAAAB7vhRQAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAECUlEQVR4nO2aQW/aSBTH/4N82n6B7qW9UImVcu4GsZzai1eCWz4AWnGwFHEg36FaVaoPVlRX8iEfoDeQ1vcQCSnt3ioFFd+3VyTCIRk8e7AhhthhWDC8bN7vEGLP88wTf7+ZQfMXUkowVCnsOwHmIVge0rA8pGF5SMPykIblIQ3LQxqWhzQsD2lYHtKwPKRheUhj6ARdXFw80Hp4eGgYWv0w66L7tf5WqUBACAHEf8MwLBQKX7/+3ev1Xv/6+tlPz3JM86miNbkppRQUFJRS0SWAQqEA4Pb2tlKpfLn8Mh6Pc030aaIlTxiGqQoBuL6+Pj8/l1JeXl5ukIZvGZavE1V1gg2GWR+9xHJDa3JTSs0FEYAChBBKKSHE27dvfvzz4+bm5vtwmGuiT5M1qudOJaXmNaSUev7z8xcvX4RhmHOqKwmc6l5f9RzQqp5YnjuEgFKAgIDIKTEG0Kye6XSaKJ6oZpb3C9Pp9P6DgVM1YuavtW8ZCVavJYn4mpfdtW8ZpXYfXm3WZ9rYW01sF6yzNZjLApWUKfonZXILnEb7oCsjXBMAfMuoYXar21w5clZ84HzA2eyuV7N8mK4c2GU0u1L2WsWUgO0mtiN05UkqMlcKic8UeYqvDuDVku+h3/HK9okZX5n1VV9DZnyx5bYQVUCypBJjPxSweWK7QkseKeWCLPcUgkKaIct0pZTyDI1tTxaBUzWMRlQeA7u8dkBuiW0b7Z+ly1PaUiGpxb0DACBwHB9AsdUb2OX+1RAw681++4M/a36X+uInyIofXvXL9lmrCCD463P//oOpAYFTjcTYPLFdoSXPaDRSWF50lgpoNBotP1ZsvepE62ypfdB1TQCm2216tehmA0cr55CMePPERrsU3b06mBVH8fejcrw1SA/YYmI7Qui4RE9PT/9oNKJ9tBBLH0IICCE+up+Oj4/XG9y3jHe/DHqt4n9MPjfIJKZVPZPJJLGLTu6q7y4nk8nGySxubdM2xE8NrZ+l4/H4z/fv804lWrHd/Id5RGhNbsy+4NNS0rA8pGF5SMPykIatIKRhKwhp2ApCmkdlBUkh6wB76xaO/XhC2ApCGuJWkP+hu2Mt2ApCGspWEH13x12fGetQ5mjEPSGUrSC67g6v1qlnuT7io5uoeXD0ubTQTN4TQtsKsthdlrujGR14AjBP7LLXSQrgdzz043NTo9Tu49swUQzkPSGPxwqy0v6RRdmOi0dKGdVhThnmAG0rSJJs+8e8XgKn0e4362aibWFE+Jbl4zF5QohbQbTcHU0khpGuudCD6Q7sb/GIRqe+2EreE8JWkAfZd4ZsBSENW0FIw1YQ0vBpKWlYHtKwPKRheUjD8pCG5SENy0Maloc0LA9pWB7SsDyk+RcNK/6FWGQO+gAAAABJRU5ErkJggg==)

Sql\_table.sql:记录表结构

Sql\_data.sql:记录测试数据,在实际企业开发中,最后提供一个初始化数据脚本。

![](data:image/*;base64,iVBORw0KGgoAAAANSUhEUgAAAJkAAABxCAIAAABJKZYCAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAIm0lEQVR4nO2dT2zbVBzHfw+NMtb9UzXUweAAcyLRduIIOANpJ+RsQBDSmLh0jNURUrXkUnoBcaCcKiT7gpQMpgXtVGBEQ8QaoqFsTcRUZdqkbkK1t0m0nYYG1f4PWiRzcJw4ie0ktpO8PL/PyXae/dz8+vv5vff7+hekqipQiOCRTt8AxTOoLcmB2pIc1rm/xD//PThz5ftLN85dXZn/6/7yvX9vA8DGx7Zs693xXN/Q4PYXX9351vp1G9x3RLEHuR/7fPDd7qVbik2DZ7YGvnj7rMteAABAEUOByx+pCc6LixGHBzF238D723qftPp0W++TewcOue+FUhcPYmxhcXpsT6K3Z9PiLfnOPysPVu8CwIaeTZvX9z2zNXB/9e43F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lgNyehugLHUL8kB2pLHZfaAAzALsZSXYFjqF+SA3Z+CVRX4JhOLyJW0y26AgzrGBAZY1uvK8CyjgGJMbYNugIs6xjgaMtu0BXgWMeAxBjbBl0BlnUMcPTLLtAV4FnHoK0jrQagugLHkBhj/Qp2uWiqK3AMdrakOIbGWHKgtiQH7OYkVFfgGOqX5EBtSRCdnuBWQ9cKHEP9khywG/sA1RU4ptOBoRrvfgehettMCdBIXYKuwTcx1kQJ0Ehdgm7CNzG2VgnQiH6gq8DRlq35HYQaJQC4q0uAH6TG2PkFBcBYE6BWCeDslxEwBke/dBtjmVhKmNKKApRrApgoARqpS9BVdGrQZQVdK3AMqTHWj2CXi6a6AsdgZ0uKY2iMJQdqS3KgtiQHakty+B8MHoV0MPQPNwAAAABJRU5ErkJggg==)

### **1.3 jdbc程序**

使用jdbc查询mysql数据库中用户表的记录。

创建java工程,加入jar包

数据库驱动包;

![](data:image/*;base64,iVBORw0KGgoAAAANSUhEUgAAAPkAAAA4CAIAAACwmcnWAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAIG0lEQVR4nO2cPWzbRhTHHxU6AZKlU5su6UIjTqG0WxrBFTo0Cw1QQAdNnYRCA4FAg9zJU7sYRQBrIIKqAAdvXbRJgLh1iAIITdKhhRoL8E3pBzpWgG0UMS124Dd55JGyRNvk+w12RN+7e3d8evfI8E9O13VAkAJQumgHECQjMNaRooCxjhQFjHWkKGCsI0UBYx0pChjrSFHAWEeKAsY6UhR474ezufHTr/+8fjP79/jtRTmEICuC8z4j0P/5zxtrpYcb775za+0CfUKQVeDL67+/mX39xYdrPGcYhnnk+fPnMcYPHz7keT6mAYJcHnyR+vb07PpayQl0k083N4EDjuMArJ/z+bxUKr169ctoNHrwyYNbN29l6TGCLAbj2tQwDAMMMMD8Apg/S6USAJyenm5ubr588fLo6CgDRxHknDBifT6fU8MdAI6Pj589e6br+osXL1bupg9N5mUt2yFXDlGqfFUhF+1GOohSpZ2JhHPJ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94eh+TK6tJsPeOIO6PS/gci2XrLTVBVQQnwdcrhWAWg2kKKCuFCkKGOtIUcBYR4oCxjpSFDDWkaKAsY4Uhf8BT3RoSdOkXc0AAAAASUVORK5CYII=)

mysql-connector-java-5.1.7-bin.jar是mysql的数据库驱动包,ojdbc14-10.2.0.1.0.jar是oracle的数据库驱动包

程序代码:

![](data:image/*;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAAAmCAIAAADFr6fFAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHfElEQVR4nO2af2gb5xnHnzeOXceJYzuO7CxSQll1aleUmVK6tuduXd2U9pQWLBbspgzk/kBamIkEnXFTQktHGEvNigQdRaINMYWGqCERBOvWjXgpbQ5C6mQ/XENzgjRz4jqpYzlxJEe+0739404nWT9OP3xneZU+3B9+33ve5967x8/7vnrfL8IYQ5XKYE25O1Bl5dAw2H/aa9PEb8jTiTo9IU18lxPakXitkKcTOWj1n7BWRV9fDr2KMQACwAAAU+zXX7z7qlysra9/bN9fVXycdtAOZPEtrbIHsZdSzfvBB9mzTqM67opAzcxe5KCJMOjM97WYttW21G9rb24i9G3m+1pM22qa6hfuRNV5jNF5FoufSpUMyOKE8mKMMcZBO9iD4p9FRFqrvFw+hWb2mTNnzp07Nzg4qGDDc/Fmg6Fh44a7d6I8z+m3bGrWGxqaGu9GorGFu9Hr82p0uErpFJTZo6Oj/f39hw8fVjbj+XgsEl2Yj8QiUS7Gzd2OxKILC/ORWGSBX+R4Pp67Ke1AEp2ekDR9eRwpNSlImUM7EOFiwGeRLdKciJYScqotrct0okzGI9RzmDFzJG+ljBNZOlA4+YM9Ojq6b9++HTt25LXkufjs1ekblydnJr+bvzkXvhW9eXX6xuXJm5NTd2ZvxXkhRzvagSzjblYcLxNzGeOa6MYYY9YNLlu296K8mHWTYA+KbTKdhDxDMJwYj30WBw0AIY/NZZZGZuyl0p0oI062Ylu2x0846OU7tECicdC+5J7PEuhO63z2D1UEeYZxOdII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NepkpaHhMN7V1XXy5Mlr167lteQFJAggXyJykRcKP0FZGUlXqUq01GxP18ctOUtPFrLJ6EpGw2DrdDqr1Xr8+HGWzXOmHxdQHGP5kirlmkKDvTKSLlWUaJn6OKPzgJ3xj4hCyRE/Yz/gNGaV0S0DbRdora2tvb29R48eDYfDCmY8Tmb27egiAGyor0tmNi4s2HTAlxCugSjpSmGppMsXoCX74WKnUDrgA0YSRSDCxcD4pRAYTWbwWYoYHYxOrxPEpJVHIKpbinZoxC9KU7KaLQNtgz07O3vs2LE9e/a0tLQomKWuxr+/FQWAxvX3yDXKw3gZJF3LV6Ll0MeJ0Q6N+EH8p81uVjoaBntmZubEiRO7d+8mCELZkheQIOCL7NTH/7jw9pG/A8CjD2wXBCxeisFecUlXCUq0TGlbVn0cADXgBv/QkB96dhkVzEpGw2CfPn3aarXq9fq8lryA4hhqatb4P/9PXMA7Hzb1/LojjkG8cgW7TJKu5SrRACC7Pk5sBD6f+UDihXKYlYpW59kMw3R0dKxfv74Q40ODT7w0cDrX3U+Gnh489KV6XSsfIU8nMXGgfNokrTKbJMkCIw05dtAKnLPVI33HTPXtspQppzysioMQXkBH/vxM7vsrE2zKi7FXI9+S9tAexGXcQKvKkiqJVXEQUmVlqAa7gvgBtfkTNHVcJ2YAAAAASUVORK5CYII=)

| **package** cn.itcast.Test\_Jdbc;//通过单独的jdbc程序,总结其中的问题 **public**** class **Jdbc\_Test {      ** public ****static**** void**main(String[] args) {                //数据库连接                Connection connection =**null **;                //预编译的Statement                /\* 使用预编译的Statement提高数据库性能。因为statement发送一个sql语句,数据库会将该sql语句进行编译, 并将编译后的结果保存到数据库的缓存中;下次在发送sql语句时,若sql语句相同,则不会再编译,而是直接使用。\*/                PreparedStatement preparedStatement =** null **;                //结果集                ResultSet resultSet =** null **;              ** try **{                      ** // ****1.**** 加载数据库驱动**(注:这个驱动在数据库驱动包(jar包)里面找)                        Class._forName_("com.mysql.jdbc.Driver");                      **// ****2.**** 通过驱动管理类获取数据库链接**                        connection =  DriverManager._getConnection_("jdbc:mysql://localhost:3306/tmb\_temp?characterEncoding=utf-8", "root", "root");                      **// ****3.**** 定义sql语句 ,其中?表示占位符 **                  String sql = "select \* from user where username = ?";                      ** // ****4.**** 获取预处理statement**                        preparedStatement = connection.prepareStatement(sql);                      **// ****5.**** 设置参数**,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值                        preparedStatement.setString(1, "王五");                      **//6.向数据库发出sql执行查询,查询出结果集**                        resultSet =  preparedStatement.executeQuery();                      **// ****7.**** 遍历查询结果集 **                      ** while**(resultSet.next()){                            System.**out**.println(resultSet.getString("id")+"  "                                +resultSet.getString("username"));                        }                }**catch**(Exception e) {                        e.printStackTrace();                }**finally **{                      ** // ****8.**** 释放资源(释放顺序:resultSet--\>preparedStatement--\>connection) **                      ** if**(resultSet!=**null**){                              **try**{resultSet.close();                                }**catch**(SQLException e) {e.printStackTrace();}                        }                      **if**(preparedStatement!=**null**){                              **try**{preparedStatement.close();                                }**catch**(SQLException e) {e.printStackTrace();}                        }                      **if**(connection!=**null**){                              **try**{connection.close();                                }**catch** (SQLException e) {e.printStackTrace();}                        }                }        }} |

| --- |

使用jdbc的原始方法(未经封装)实现了查询数据库表记录的操作。

### **1.4 jdbc编程步骤**

1. 1、加载数据库驱动

2. 2、创建并获取数据库链接

3. 3、创建jdbc statement对象

4. 4、设置sql语句

5. 5、设置sql语句中的参数(使用preparedStatement)

6. 6、通过statement执行sql并获取结果

7. 7、对sql执行结果进行解析处理

8. 8、释放资源(resultSet、preparedstatement、connection)

### **1.5 问题总结**

① 数据库连接,使用时创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,          影响数据库的性能。

**设想的解决方案:** 使用数据库连接池管理数据库连接。

② 将sql语句硬编码到java代码中,若sql语句修改,则需要重新编译java代码,不利于系统维护。

**设想的解决方案:** 将sql语句配置在xml配置文件中,即使sql变化,不需要对java代码进行重新编译。

③ 向preparedStatement中设置参数,对占位符号位置和设置参数值,硬编码在java代码中,不利于系统维护。

**设想的解决方案:** 将sql语句及占位符号和参数全部配置在xml中。

④从resultSet中遍历结果集数据时,存在硬编码,将获取表的字段进行硬编码,不利于系统维护。

**设想的解决方案:** 将查询的结果集,自动映射成java对象。

你可能感兴趣的:(1.原生态JDBC编程中的问题总结)