JBDC入门

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_40662281/article/details/79951140
  • 导包:
    这里写图片描述
  • 数据库
CREATE DATABASE mydb;
#使用数据库
USE mydb;
###创建分类表
CREATE TABLE category(
  cid INT PRIMARY KEY AUTO_INCREMENT  ,
  cname VARCHAR(100)
);
#初始化数据
INSERT INTO category (cname) VALUES('家电');
INSERT INTO category (cname) VALUES('服饰');
INSERT INTO category (cname) VALUES('化妆品');
  • 获取数据库数据
package bull01.JBDCBasis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

public class JDBCDemo_01 {
    @Test
    public void method1() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得连接,三个参数url,user,password
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
        //3.获得语句执行者
        Statement st = conn.createStatement();
        //4.执行SQL语句
        ResultSet rs = st.executeQuery("select * from category");
        //5.处理结果集
        /*
         * rs.next()指向下一行。true:有效;false:尾。
         * rs.previous()指向上一行。true:有效;false:头。
         */
        while(rs.next()) {
            Integer cid = rs.getInt("cid");
            String cname = rs.getString("cname");
            System.out.println(cid+"---"+cname);
        }
        //6.释放资源
        /*
         * 后开先关
         * 从下往上关
         */
        rs.close();
        st.close();
        conn.close();
    }
}
  • 不抛去异常,采用try···catch方式
package bull01.JBDCBasis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class JDBCDemo_02 {
    @Test
    public void method1()  {
        //1.注册驱动
        //2.获得连接,三个参数url,user,password
        Connection conn = null;
        //3.获得语句执行者
        Statement st = null;
        //4.执行SQL语句
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
            st = conn.createStatement();
            rs = st.executeQuery("select * from category");
            //5.处理结果集
            /*
             * rs.next()指向下一行。true:有效;false:尾。
             * rs.previous()指向上一行。true:有效;false:头。
             */
            while(rs.next()) {
                Integer cid = rs.getInt("cid");
                String cname = rs.getString("cname");
                System.out.println(cid+"---"+cname);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //6.释放资源
        /*
         * 后开先关
         * 从下往上关
         */
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

JBDC连接问题出现异常!!

11-09

[code=java]rnpackage com;rnrnimport java.sql.*;rnrnimport javax.sql.*;rnrnpublic class Test rn public static void main(String[] args)rn Connection conn = null;rn Statement stmt = null;rn ResultSet rs = null;rn rn //1。注册驱动rn try rn Class.forName("oracle.jdbc.driver.OracleDriver");rn rn //2。通过驱动管理器建立连接rn String url = "jdbc:oracle:thin:@192.168.0.106:1521:dylanfire";rn String dbUsername = "scott";rn String dbPassword = "dylan";rn conn = DriverManager.getConnection(url, dbUsername, dbPassword);rn rn //3.建立连接对象rn stmt = conn.createStatement();rn rn //4、通过对象传递数据,并返回结果rn String sql = "select ename, job, sal, hiredate from emp";rn rs = stmt.executeQuery(sql);rn while(rs.next())rn String ename = rs.getString("ename");rn String job = rs.getString("job");rn Double sal = rs.getDouble("sal");rn java.sql.Date hiredate = rs.getDate("hiredate");rn System.out.println(ename + ", " + job + ", " + sal + ", " + hiredate);rn rn catch (ClassNotFoundException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finallyrn try rn rs.close();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn try rn stmt.close();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn try rn conn.close();rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn rnrnrnrn[/code]rn[code=objc]oracle服务器的server-name是dylanfirern192.168.0.106是局域网安装oracle数据库的IP。rn我通过telnet可以访问到oracle的数据库rn不知道为什么通过JAVA就不可以 rnrnrneclipse给出的异常为:rnjava.sql.SQLException: The Network Adapter could not establish the connectionrn at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:451)rn at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:535)rn at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:218)rn at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)rn at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)rn at java.sql.DriverManager.getConnection(Unknown Source)rn at java.sql.DriverManager.getConnection(Unknown Source)rn at com.Test.main(Test.java:21)rnCaused by: oracle.net.ns.NetException: The Network Adapter could not establish the connectionrn at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:356)rn at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:419)rn at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:811)rn at oracle.net.ns.NSProtocol.connect(NSProtocol.java:249)rn at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1407)rn at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:328)rn ... 7 morernCaused by: java.net.ConnectException: Connection refused: connectrn at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)rn at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)rn at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)rn at java.net.AbstractPlainSocketImpl.connect(Unknown Source)rn at java.net.PlainSocketImpl.connect(Unknown Source)rn at java.net.SocksSocketImpl.connect(Unknown Source)rn at java.net.Socket.connect(Unknown Source)rn at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:138)rn at oracle.net.nt.ConnOption.connect(ConnOption.java:120)rn at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:334)rn ... 12 morernException in thread "main" java.lang.NullPointerExceptionrn at com.Test.main(Test.java:44)rnrnrnrn希望各位大侠帮我看看。有什么可能出现这种问题的情况rn我一个一个的排查rnrn[/code]

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭