欢迎来到天天文库
浏览记录
ID:35294606
大小:119.50 KB
页数:14页
时间:2019-03-23
《jdbc操作数据库汇总》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、一、SQL语言回顾1、Selectselect*fromTwhere…2、InsertinsertintoTvalues(…)3、CreatecreatetableT(…)4、DeletedeletefromTwhere…5、UpdateupdateTsett1=…andt2=..wheret3=…6、DropdroptableT二、JDBC编程步骤1、LoadtheDriverClass.forName()
2、Class.forName().newInstance()
3、newDriverName()实例化时自动向DriverMan
4、ager注册,不需显式调用DriverManager.registerDriver方法2、ConnecttotheDataBaseDriverManager.getConnection()3、ExecutetheSQLConnection.CreateStatement()Statement.executeQuery()Statement.executeUpdate()4、Retrievetheresultdata循环取得结果while(rs.next())5、Showtheresultdata将数据库中的各种类型转换为Java中
5、的类型(getXXX)方法6、CloseClosetheresultset./closethestatement/closetheconnection三、JDBC操作oracle数据库1、JDBC连接oracle数据库:importjava.sql.*;publicclassTestJDBC{publicstaticvoidmain(String[]args)throwsClassNotFoundException,SQLException{Class.forName("oracle.jdbc.driver.OracleDrive
6、r");//neworacle.jdbc.driver.OracleDriver();Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:solid","scott","tiger");}}2、向数据库中取字段,遍历数据库,并显示出相应字段:importjava.sql.*;publicclassTestJDBC{publicstaticvoidmain(String[]args)throwsClassNotFoundExcept
7、ion,SQLException{Class.forName("oracle.jdbc.driver.OracleDriver");//neworacle.jdbc.driver.OracleDriver();Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:solid","scott","tiger");Statementstmt=conn.createStatement();ResultSetrs=stmt.executeQ
8、uery("select*fromdept");while(rs.next()){System.out.println(rs.getString("deptno"));System.out.println(rs.getInt("deptno"));}rs.close();stmt.close();conn.close();}}3、捕捉相关异常,完善JDBC编程:importjava.sql.*;publicclassTestJDBC{publicstaticvoidmain(String[]args)throwsClassNotF
9、oundException,SQLException{ResultSetrs=null;Statementstmt=null;Connectionconn=null;try{Class.forName("oracle.jdbc.driver.OracleDriver");conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:solid","scott","tiger");stmt=conn.createStatement();rs=stmt.execu
10、teQuery("select*fromdept");while(rs.next()){System.out.println(rs.getString("deptno"));System.out.println(rs.getInt("deptno"
此文档下载收益归作者所有