收藏 分销(赏)

java连接数据库步骤.docx

上传人:精*** 文档编号:2153122 上传时间:2024-05-21 格式:DOCX 页数:4 大小:22.39KB 下载积分:5 金币
下载 相关 举报
java连接数据库步骤.docx_第1页
第1页 / 共4页
java连接数据库步骤.docx_第2页
第2页 / 共4页


点击查看更多>>
资源描述
<p>______________________________________________________________________________________________________________ 完整java开发中JDBC连接数据库代码和步骤 JDBC连接数据库 &nbsp; •创建一个以JDBC连接数据库的程序,包含7个步骤: &nbsp; 1、加载JDBC驱动程序: &nbsp; &nbsp; &nbsp;在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), &nbsp; &nbsp; &nbsp;这通过java.lang.Class类的静态方法forName(String &nbsp;className)实现。 &nbsp; &nbsp; &nbsp;例如: &nbsp; &nbsp; &nbsp;try{ &nbsp; &nbsp; &nbsp;//加载MySql的驱动类 &nbsp; &nbsp; &nbsp;Class.forName(&quot;com.mysql.jdbc.Driver&quot;) ; &nbsp; &nbsp; &nbsp;}catch(ClassNotFoundException e){ &nbsp; &nbsp; &nbsp;System.out.println(&quot;找不到驱动程序类 ,加载驱动失败!&quot;); &nbsp; &nbsp; &nbsp;e.printStackTrace() ; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; 成功加载后,会将Driver类的实例注册到DriverManager类中。 &nbsp; 2、提供JDBC连接的URL &nbsp; &nbsp; •连接URL定义了连接数据库时的协议、子协议、数据源标识。 &nbsp; &nbsp; &nbsp;•书写形式:协议:子协议:数据源标识 &nbsp; &nbsp; &nbsp;协议:在JDBC中总是以jdbc开始 &nbsp; &nbsp; &nbsp;子协议:是桥连接的驱动程序或是数据库管理系统名称。 &nbsp; &nbsp; &nbsp;数据源标识:标记找到数据库来源的地址与连接端口。 &nbsp; &nbsp; &nbsp;例如:(MySql的连接URL) &nbsp; &nbsp; &nbsp;jdbc:mysql: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//localhost:3306/test?useUnicode=true&amp;characterEncoding=gbk ; &nbsp; &nbsp; useUnicode=true:表示使用Unicode字符集。如果characterEncoding设置为 &nbsp; &nbsp; gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式。 &nbsp; 3、创建数据库的连接 &nbsp; &nbsp; &nbsp;•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象, &nbsp; &nbsp; &nbsp; 该对象就代表一个数据库的连接。 &nbsp; &nbsp; &nbsp;•使用DriverManager的getConnectin(String url , String username , &nbsp; &nbsp; &nbsp; &nbsp;String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和 &nbsp; &nbsp; &nbsp; 密码来获得。 &nbsp; &nbsp; &nbsp; 例如: &nbsp; &nbsp; &nbsp; //连接MySql数据库,用户名和密码都是root &nbsp; &nbsp; &nbsp; String url = &quot;jdbc:mysql://localhost:3306/test&quot; ; &nbsp; &nbsp; &nbsp; &nbsp; String username = &quot;root&quot; ; &nbsp; &nbsp; &nbsp; String password = &quot;root&quot; ; &nbsp; &nbsp; &nbsp; try{ &nbsp; &nbsp; &nbsp;Connection con = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DriverManager.getConnection(url , username , password ) ; &nbsp; &nbsp; &nbsp; }catch(SQLException se){ &nbsp; &nbsp; &nbsp;System.out.println(&quot;数据库连接失败!&quot;); &nbsp; &nbsp; &nbsp;se.printStackTrace() ; &nbsp; &nbsp; &nbsp; } &nbsp; 4、创建一个Statement &nbsp; &nbsp; &nbsp;•要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3 &nbsp; &nbsp; &nbsp; 种类型: &nbsp; &nbsp; &nbsp; &nbsp;1、执行静态SQL语句。通常通过Statement实例实现。 &nbsp; &nbsp; &nbsp; &nbsp;2、执行动态SQL语句。通常通过PreparedStatement实例实现。 &nbsp; &nbsp; &nbsp; &nbsp;3、执行数据库存储过程。通常通过CallableStatement实例实现。 &nbsp; &nbsp; &nbsp;具体的实现方式: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Statement stmt = con.createStatement() ; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement pstmt = con.prepareStatement(sql) ; &nbsp; &nbsp; &nbsp; &nbsp; CallableStatement cstmt = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con.prepareCall(&quot;{CALL demoSp(? , ?)}&quot;) ; &nbsp; 5、执行SQL语句 &nbsp; &nbsp; &nbsp;Statement接口提供了三种执行SQL语句的方法:executeQuery 、executeUpdate &nbsp; &nbsp; 和execute &nbsp; &nbsp; &nbsp;1、ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;,返回一个结果集(ResultSet)对象。 &nbsp; &nbsp; &nbsp; 2、int executeUpdate(String sqlString):用于执行INSERT、UPDATE或 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等 &nbsp; &nbsp; &nbsp; 3、execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语句。 &nbsp; &nbsp; 具体实现的代码: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM ...&quot;) ; &nbsp; &nbsp; &nbsp;int rows = stmt.executeUpdate(&quot;INSERT INTO ...&quot;) ; &nbsp; &nbsp; &nbsp;boolean flag = stmt.execute(String sql) ; &nbsp; 6、处理结果 &nbsp; &nbsp; &nbsp;两种情况: &nbsp; &nbsp; &nbsp; 1、执行更新返回的是本次操作影响到的记录数。 &nbsp; &nbsp; &nbsp; 2、执行查询返回的结果是一个ResultSet对象。 &nbsp; &nbsp; &nbsp;• ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些 &nbsp; &nbsp; &nbsp; &nbsp;行中数据的访问。 &nbsp; &nbsp; &nbsp;• 使用结果集(ResultSet)对象的访问方法获取数据: &nbsp; &nbsp; &nbsp; while(rs.next()){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = rs.getString(&quot;name&quot;) ; &nbsp; &nbsp; &nbsp;String pass = rs.getString(1) ; // 此方法比较高效 &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp;(列是从左到右编号的,并且从列1开始) &nbsp; 7、关闭JDBC对象 &nbsp; &nbsp; &nbsp; &nbsp; 操作完成以后要把所有使用的JDBC对象全都关闭,以释放JDBC资源,关闭顺序和声 &nbsp; &nbsp; &nbsp; 明顺序相反: &nbsp; &nbsp; &nbsp; 1、关闭记录集 &nbsp; &nbsp; &nbsp; 2、关闭声明 &nbsp; &nbsp; &nbsp; 3、关闭连接对象 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(rs != null){ &nbsp; // 关闭记录集 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.close() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}catch(SQLException e){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(stmt != null){ &nbsp; // 关闭声明 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stmt.close() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}catch(SQLException e){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(conn != null){ &nbsp;// 关闭连接对象 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conn.close() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch(SQLException e){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace() ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; Welcome To Download !!! 欢迎您的下载,资料仅供参考! 精品资料</p>
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 数据库/数据算法

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服