当前位置:Gxlcms > 数据库问题 > jdbc连接数据库

jdbc连接数据库

时间:2021-07-01 10:21:17 帮助过:39人阅读

   Connection conn = null;

   PreparedStatement pre = null;

   ResultSet result = null;

   try{

       Class.forName("oracle.jdbc.driver.OracleDriver");

       System.out.println("开始尝试连接数据库!");

       conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123456");

       if (conn!=null){

        System.out.println("连接成功!");

       }else{

        System.out.println("连接失败!");

       }

       Statement stm=conn.createStatement();

       StringBuffer sql=new StringBuffer();

       sql.append("create table woaini (");   //创建表

       sql.append("id int primary key,");

       sql.append("name varchar2(100)");

       sql.append(")");

       System.err.println("SQL ->"+sql);

       stm.execute(sql.toString());   //发送sql语句

       

   }catch (Exception e){

       e.printStackTrace();

   }

   

   

   finally{

       try{

           if (result != null)

               result.close();

           if (pre != null)

               pre.close();

           if (conn != null)

               conn.close();

           System.out.println("数据库连接已关闭!");

       }

       catch (Exception e){

           e.printStackTrace();

       }

       return conn;

   }

}

}


jdbc连接数据库

标签:连接数据库   package   public   import   result   

人气教程排行