最近在自学mysql数据库,同时将来职场上的开发语言为java,所以就尝试在eclipse环境下写个小的事务处理的数据库程序。在这里谈谈java mysql数据库应用程序的开发。首先应该在本地机器上安装mysql数据库,具体安装过程见新手上路中提供的链接;然后最好安装辅
最近在自学mysql数据库,同时将来职场上的开发语言为java,所以就尝试在eclipse环境下写个小的事务处理的数据库程序。在这里谈谈java mysql数据库应用程序的开发。首先应该在本地机器上安装mysql数据库,具体安装过程见新手上路中提供的链接;然后最好安装辅助的可视化工具:heidisql,具体过程见heidisql文中提供的链接。然互就是mysql jdbc驱动的安装了,在这里提供一个链接:mysql jdbc驱动 。
按照上述的步骤,剩下的就是应用程序的编写,jdbc编程中相关流程是通用的,应该着重看看,在此我谈谈我的理解。
jdbc: java database connection。jdbc是一组编程接口,数据库系统的底层开发者实现接口,java开发者调用jdbc提供的接口进行与数据库的创建、链接、更新等操作。jdbc提供两种api,分别是面向开发人员的api和面向底层的jdbc驱动程序api,底层主要通过直接的jdbc驱动和jdbc-odbc桥驱动实现与数据库的连接。
1)、加载数据库驱动程序;
class.forname(driver)//此处driver指驱动的路径
2)、建立数据库连接;
connection con = drivermanager.getconnection(url, user, password);//以特定的用户访问指定的数据库
3)、操作数据库,执行sql语句;
4)、断开数据库连接。
下面转一段介绍, 完整java开发中jdbc连接数据库代码和步骤,在此表示感谢:
1、加载jdbc驱动程序:
在连接数据库之前,首先要加载想要连接的数据库的驱动到jvm(java虚拟机),这通过java.lang.class类的静态方法forname(string classname)实现。
例如:
try{ //加载mysql的驱动类 class.forname(com.mysql.jdbc.driver) ; }catch(classnotfoundexception e){ system.out.println(找不到驱动程序类 ,加载驱动失败!); e.printstacktrace() ; }
成功加载后,会将driver类的实例注册到drivermanager类中。
2、提供jdbc连接的url
连接url定义了连接数据库时的协议、子协议、数据源标识。书写形式:协议:子协议:数据源标识;协议:在jdbc中总是以jdbc开始;子协议:是桥连接的驱动程序或是数据库管理系统名称;数据源标识:标记找到数据库来源的地址与连接端口。
例如:(mysql的连接url)
jdbc:mysql: //localhost:3306/test?useunicode=true&characterencoding=gbk ; useunicode=true:表示使用unicode字符集。如果characterencoding设置为 gb2312或gbk,本参数必须设置为true 。characterencoding=gbk:字符编码方式。
3、创建数据库的连接
要连接数据库,需要向java.sql.drivermanager请求并获得connection对象,该对象就代表一个数据库的连接。使用drivermanager的getconnectin(string url,string username,string password )方法传入指定的欲连接的数据库的路径、数据库的用户名和密码来获得。
例如:
//连接mysql数据库,用户名和密码都是root string url = jdbc:mysql://localhost:3306/test ; string username = root ; string password = root ; try{ connection con = drivermanager.getconnection(url , username , password ) ; }catch(sqlexception se){ system.out.println(数据库连接失败!); se.printstacktrace() ; }
4、创建一个statement
要执行sql语句,必须获得java.sql.statement实例,statement实例分为以下3种类型:
1)、执行静态sql语句。通常通过statement实例实现。
2)、执行动态sql语句。通常通过preparedstatement实例实现。
3)、执行数据库存储过程。通常通过callablestatement实例实现。
具体的实现方式: statement stmt = con.createstatement() ; preparedstatement pstmt = con.preparestatement(sql) ; callablestatement cstmt = con.preparecall({call demosp(? , ?)}) ;
5、执行sql语句
statement接口提供了三种执行sql语句的方法:executequery 、executeupdate和execute
1)、resultset executequery(string sqlstring):执行查询数据库的sql语句,返回一个结果集(resultset)对象。
2)、int executeupdate(string sqlstring):用于执行insert、update或delete语句以及sql ddl语句,如:create table和drop table等
3)、execute(sqlstring):用于执行返回多个结果集、多个更新计数或二者组合的语句。
具体实现的代码:
resultset rs = stmt.executequery(select * from ...) ; int rows = stmt.executeupdate(insert into ...) ; boolean flag = stmt.execute(string sql) ;
6、处理结果
两种情况:
1)、执行更新返回的是本次操作影响到的记录数。
2)、执行查询返回的结果是一个resultset对象。
resultset包含符合sql语句中条件的所有行,并且它通过一套get方法提供了对这些行中数据的访问。使用结果集(resultset)对象的访问方法获取数据: while(rs.next()){ string name = rs.getstring(name) ; string pass = rs.getstring(1) ; // 此方法比较高效 } (列是从左到右编号的,并且从列1开始)
7、关闭jdbc对象
操作完成以后要把所有使用的jdbc对象全都关闭,以释放jdbc资源,关闭顺序和声明顺序相反:
1)、关闭记录集
2)、关闭声明
3)、关闭连接对象-数据库
if(rs != null){ // 关闭记录集 try{ rs.close() ; }catch(sqlexception e){ e.printstacktrace() ; } } if(stmt != null){ // 关闭声明 try{ stmt.close() ; }catch(sqlexception e){ e.printstacktrace() ; } } if(conn != null){ // 关闭连接对象 try{ conn.close() ; }catch(sqlexception e){ e.printstacktrace() ; } }
上面那段文字介绍的挺好的,还有我自己也是初学,就直接转载了,在此表示感谢!下面贴上我的一段入门级代码,要毕业了,马上要工作了,想多接触一点工作上可能用得到的jdbc数据库开发,有没有时间写个很大的项目,所以就只是描述一下大致流程,见笑了!
import java.sql.*;public class jdbcdemo { public static void main(string[] args) { string user = root; string password = 199203211410xfcy; string url = jdbc:mysql://localhost:3306/studentdb;//建立数据库服务器的地址 string tablename = student_information; string driver = com.mysql.jdbc.driver; string sqlsentence; connection con = null;//连接对象 statement stmt = null;//操作对象 resultset rs = null;//查询结果 try { class.forname(driver);//加载数据库驱动程序driver类 con = drivermanager.getconnection(url, user, password);//数据库连接,以特定的用户访问指定的数据库 stmt = con.createstatement(); sqlsentence = insert into + tablename + values (9,'honey',21); stmt.executeupdate(sqlsentence); sqlsentence = select * from + tablename; rs = stmt.executequery(sqlsentence); resultsetmetadata rsmd = rs.getmetadata(); int j = 0; j = rsmd.getcolumncount(); for (int k = 0; k < j; k++) { system.out.print(rsmd.getcolumnname(k + 1)); system.out.print(\t); } system.out.println(); while (rs.next()) { for (int i = 0; i < j; i++) { system.out.print(rs.getstring(i + 1)); system.out.print(\t); } system.out.println(); } } catch (classnotfoundexception e1) { system.out.println(数据库驱动不存在!); system.out.println(e1.tostring()); } catch (sqlexception e2) { system.out.println(数据库存在异常!); system.out.println(e2.tostring()); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (sqlexception e) { system.out.println(e.tostring()); } } }}
前提是要有一个studentdb的数据库,和一张属性一致的student_information表。
由于时间有限,在写博文的过程中参考过一些文献,在此表示感谢;同时鉴于水平原因,你难免有不足之处,欢迎斧正!