Java 通过jdbc 驱动形式连接Oracle数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_26570215/article/details/83544170

本人小白,初学Java jdbc连接  害怕自己忘记相关知识,特此记录一下:

首先,创建一个普通的Java工程,然后将Oracle 目录下的jdbc.jar包拖进工程根目录下,如图所示

然后需要将将此jar包build进工程中:

(拖进去之后可以右键add build)

准备工作就算完成了.

开始写

心中要明确jdbc是如何连接数据库的

大约就是:注册驱动  Class.forName("oracle驱动")

获取连接:Connection conn=DriverManger.getConnection("url","user","pwd");

通过获取的连接创建Statement: Statement s=conn.createStatement();

利用statement执行SQL,如果是查询返回ResultSet:ResultSet rs=s.executeQuery("sql");

遍历结果集:rs.next()      rs.getString("字段....")

注意:获取连接要捕获异常.

最后不管执行是否成功,都要关闭连接  :   conn.close()

截屏如下:

展开阅读全文

Java通过JDBC连接oracle数据库的问题

09-30

以前在JDK131做的一个小程序,连接oracle8.1.6,很正常,现在用JDK141编译没问题,执行则报错(在editplus下编译,执行)rnrn---------- Java ----------rn没有相应的数据库驱动:oracle.jdbc.driver.OracleDriverrn数据库连接出错:No suitable driverrnjava.lang.NullPointerExceptionrn at myFirstJDBC.main(myFirstJDBC.java:39)rnException in thread "main" rn输出完成 (耗时 0 秒) - 正常终止rnrnrnrn源代码如下:rnrnimport java.io.*;rnimport java.sql.*;rnrnpublic class myFirstJDBC rnrn public static void main(String[] args) rn rn Connection con=null;rn PreparedStatement ps=null;rn ResultSet rs=null;rnrn try //载入数据库驱动类库rn rn Class.forName ("oracle.jdbc.driver.OracleDriver"); //rn rn catch (ClassNotFoundException ce)rn rn //System.out.println("SQLException:"+ce.getMessage());rn System.out.println("没有相应的数据库驱动:"+ce.getMessage());rn rn rn try rn rn con=DriverManager.getConnection("jdbc:oracle:thin:@10.64.21.14:1521:baa","baa","baa");//通过rn rn catch (SQLException e)rn rn System.out.println("数据库连接出错:"+e.getMessage());rn rn rn rn try //执行sql语句rn rn String sql="select * from a where a_id=2";rn ps=con.prepareStatement(sql);rn rs=ps.executeQuery();rn while (rs.next())rn rn //System.out.println(rs.getString("area_name"));rn System.out.println(rs.getString(2));//与上句一样rn rn rs.close();rn ps.close();rn rn catch (SQLException e)rn rn //System.out.println("SQLException:"+e.getMessage());rn System.out.println("SQL语句执行有误:"+e.getMessage());rn rn rnrn请各位指教,谢谢!rnrn 论坛

jsp通过jdbc 连接oracle数据库出现的问题?

11-23

大家帮我看一下我的这段连接数据库的 代码那里错了? rnrnrnrn rn rn <%@ page language = "java" import = "java.sql.*"%> rnrnrnrnrnrn <%rn tryrnrn DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());rnrn  rn//连结数据库,格式为(”jdbc:oracle:thin:@主机host:端口号:数据库名”,”用户名”,”密码”)rnConnection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myora","sys","87409031");rnrn //创建SQL语句 rnrnStatement stmt=conn.createStatement();rnrn//处理查询结果rnrn ResultSet rset=stmt.executeQuery("select * from ding");rnrn while(rset.next())rnrn System.out.println(rset.getString(2));rn rn rset.close();rn stmt.close(); rn conn.close();rnrnSystem.out.println("linked ok"); rn catch(Exception e)rn out.println("SQLException:" + e);rn  rn%>rnrnrnrnrn错误提示;rnorg.apache.jasper.JasperException: Unable to compile class for JSPrnrnAn error occurred at line: 11 in the jsp file: /index1.jsprnrnGenerated servlet error:rn [javac] Compiling 1 source filernrnC:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\index1_jsp.java:59: illegal character: \161rn?? rn^rnrnrnrnAn error occurred at line: 11 in the jsp file: /index1.jsprnrnGenerated servlet error:rnC:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\index1_jsp.java:59: illegal character: \161rn?? rn ^rnrnrnrnAn error occurred at line: 11 in the jsp file: /index1.jsprnrnGenerated servlet error:rnC:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\index1_jsp.java:82: illegal character: \161rn ??rn ^rnrnrnrnAn error occurred at line: 11 in the jsp file: /index1.jsprnrnGenerated servlet error:rnC:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\index1_jsp.java:82: illegal character: \161rn ??rn ^rn4 errorsrnrnrn org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:83)rn org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:315)rn org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:406)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:463)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:442)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:430)rn org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:274)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:810)rn 论坛

没有更多推荐了,返回首页