JDBC数据库编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

™™™ODBC™JDBCODBCVBVCJDBCJava™¾DriverManager¾DriverManager.registerDriver(newsun.jdbc.odbc.JdbcOdbcDriver());¾DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());™ƒClass.forName(sun.jdbc.odbc.JdbcOdbcDriver);ƒClass.forName(oracle.jdbc.driver.OracleDriver);™JDBCURLjdbc:subprotocol:subname¾¾¾jdbc:mysql://ip/databaseorg.gjt.mm.mysql.DriverMySQLjdbc:oracle:thin:@[ip]:[port]:[sid]oracle.jdbc.driver.OracleDriverOraclethinDriverjdbc:odbc:[odbcsource]sun.jdbc.odbc.JdbcOdbcDriverJDBC-ODBCURL™staticConnectionconn=DriverManager.getConnection(URL,login_name,login_password);staticConnectionconn=DriverManager.getConnection(URL);™StatementSQLDBMS™StatementStatementstmt=conn.createStatement();™Statement¾executeUpdate()INSERT,UPDATE,DELETEintstmt.executeUpdate(query);¾executeQuery()SELECTResultSetstmt.executeQuery(query);™ResultSetƒResultSetrsstmt.executeQuery(query);™ResultSetSQL™getXXX¾getInt()¾getString()while(rs.next()){System.out.print(rs.getInt(id));……}0idname112233445™Statement¾stmt.close();™Connection¾conn.close();™™:¾StatementcreateStatement()¾PreparedStatementprepareStatement(Stringsql)¾commit()¾rollback()¾close()™SQL™Statement¾Statement¾PreparedStatement•Statement¾CallableStatement•PreparedStatementinterfaceStatementinterfacePreparedStatementinterfaceCallableStatement™Statement¾ResultSetexecuteQuery()•ResultSet¾intexecuteUpdate()•™StatementConnectionconn=DriverManager.getConnection(“jdbc:odbc:accessdb“,“admin”,“admin”);Statementstmt=conn.createStatement();ResultSetrst=stmt.executeQuery(select*fromsometable);™PreparedStatementConnectionconn=DriverManager.getConnection(“jdbc:odbc:accessdb“,“admin”,“admin”);PreparedStatementpstmt=conn.prepareStatement(insertintouser_infoValues(???????));pstmt.setString(1,hellking);pstmt.setInt(2,20);pstmt.setBoolean(3,true);pstmt.executeUpdate();™createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);ibmResultSetone-time-throughforward-only™ResultSetTYPE_FORWARD_ONLYTYPE_SCROLL_INSENSITIVE™TYPE_SCROLL_SENSITIVETYPE_SCROLL_INSENSITIVEResultSetResultSetResultSetResultSetCONCUR_READ_ONLYResultSetCONCUR_UPDATABLEResultSet™CREATEPROCEDUREmyProc1@noint,@namevarchar(10)outputAS™select@name=namefrompersonwhereoid=@no™GO™DbJdbcdj=newDbJdbc();™Connectionconn=dj.getConnection();™try{™™//™CallableStatementcs=conn.prepareCall({callmyProc1(?,?)});™cs.setInt(1,1);//™cs.registerOutParameter(2,Types.VARCHAR);//™cs.execute();™™™StringstrName=cs.getString(2);//™System.out.println(+strName);™™dj.closeConnection(conn);™}™catch(SQLExceptione){™e.printStackTrace();™}™Statementstatm=conn.createStatement();™ResultSetrs=statm.executeQuery(select*fromperson);™ResultSetMetaDatarmeta=rs.getMetaData();//™intnumColumns=rmeta.getColumnCount();//™//™for(inti=1;i=numColumns;i++)™{™if(inumColumns)™System.out.print(rmeta.getColumnName(i)+|);™else™System.out.println(rmeta.getColumnName(i));™}™//™while(rs.next())™{™for(inti=1;i=numColumns;i++)™{™//System.out.print(+rs.getInt(1)+|);™System.out.print(rs.getString(i)+|);™//System.out.println(rs.getString(3));™}™}™™ResultSet.next()™¾getString()varcharchar¾getFloat()Float¾getDouble()DoublePrecision¾getDate()Date¾getBoolean()Boolean¾getObject()SQL¾getBlob()Blob()¾getClob()Clob()™™publicStringgetString(intcolumnlndex)throwsSQLException™publicStringgetString(StringcolumnName)throwsSQLException™ResultSet......ResultSetrst=stmt.executeQuery(Select*fromUser_Info);while(rst.next()){System.out.println(rst.getString(userName));System.out.println(rst.getDate(regDate));System.out.println(rst.getString(4));System.out.println(rst.getInt(age));}......™importjava.sql.*;™Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);™Connectionconn=DriverManager.getConnection(jdbc:odbc:jdbc_odbc);™StatementStatementstmt=conn.createStatement();™ResultSetrs=stmt.executeQuery(SELECTid,nameFROMstudent);™while(rs.next()){......}™Stamtementstmt.close();™conn.close();™jdbccommit()rollbackjava.sql.Connection™jdbccommit()rollback()™jdbcsetAutoCommit(false)commit()commit()rollback()™try{conn=DriverManager.getConnection(jdbc:oracle:thin:@host:1521:SID,username,userpwd;conn.setAutoCommit(false);//stmt=conn.createStatement();stmt.executeUpdate(“altertable…”);//1™stmt.executeUpdate(“insertintotable…”);//2conn

1 / 38
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功