jdbc操作的五大步骤

xiaoxiao2021-02-27  578

jdbc操作的五大步骤                private static final String className = "oracle.jdbc.OracleDriver";           private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";           private static final String user = "scott";           private static final String password = "tiger";                                // 1.加载驱动程序                               Class .forName( classname );                               // 2.获取数据库连接                               Connection connection = DriverManager .getConnection( url , user , password );                               // 3.创建可以执行sql语句的对象                               Statement stmt = connection . createStatement ();                                 // 4. 执行sql语句  处理结果                               int row = stmt . executeUpdate ( sql );                               boolean f = stmt . execute(sql)                                           ResultSet rs = stmt.executeQuery(sql)                             //5.关闭连接 eg:       public class EmpUpdateTest {            static final String url = "jdbc:oracle:thin:@localhost:1521:orcl" ;            static final String user = "scott" ;            static final String password = "tiger" ;            static final String classname = "oracle.jdbc.OracleDriver" ;                       public boolean updateEmp ( String sql ) {                     boolean flag = true ;                     Connection conn = null ;                     Statement stmt = null ;                     try {                               // 1 加载                               Class .forName( classname );                               // 2 建立连接                               conn = DriverManager .getConnection( url , user , password );                               // System.out.println(conn);                               // 3.创建可执行对象                               stmt = conn . createStatement ();                               int rs = stmt . executeUpdate ( sql );                               // 4.处理结果                               if ( rs < 1 ) {                                        flag = false ;                              }                    } catch ( Exception e ) {                               // TODO Auto-generated catch block                               e . printStackTrace ();                    } finally {                               try {                                        if ( stmt != null ) {                                                  stmt . close ();                                       }                                        if ( conn != null ) {                                                  conn . close ();                                       }                                        System . out . println ( "更新成功!" );                              } catch ( SQLException e ) {                                        // TODO Auto-generated catch block                                        e . printStackTrace ();                              }                    }                     return flag ;           } }
转载请注明原文地址: https://www.6miu.com/read-979.html

最新回复(0)