Python 3 - jaydebeapi - 连接到Oracle数据库

时间:2018-04-20 13:55:18

标签: python jaydebeapi

我最近下载了jaydebeapi库,我正在努力连接到远程Oracle数据库。我正在做以下事情:

import jaydebeapi as j
j.connect('oracle.jdbc.driver.OracleDriver', ['jdbc:oracle:thin:@myhost:port:instance_name', "user", "pass"], "/path/to/ojdbc7.jar")

在这种情况下,我收到错误:

FileNotFoundError: [WinError 2] The system cannot find the file specified

虽然jar文件位于指定的文件夹中。

如果我省略了ojdbc7.jar的路径,我会收到:

import jaydebeapi as j
    j.connect('oracle.jdbc.driver.OracleDriver', ['jdbc:oracle:thin:@myhost:port:instance_name', "user", "pass"])

TypeError                                 Traceback (most recent call last)
<ipython-input-164-db519a221754> in <module>()
----> 1 j.connect('oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:@cdwprdmi-scan.int-app.stockex.com:1522:mdwprdmi1',)

L:\pyzo2015a\lib\site-packages\jaydebeapi\__init__.py in connect(jclassname, driver_args, jars, libs)
     61 
     62         gateway = java_gateway.JavaGateway.launch_gateway(
---> 63             port=25333, classpath=classpath, javaopts=javaopts, die_on_exit=True)
     64 
     65         java_gateway.java_import(gateway.jvm, 'java.sql.DriverManager')

L:\pyzo2015a\lib\site-packages\py4j\java_gateway.py in launch_gateway(cls, port, jarpath, classpath, javaopts, die_on_exit, redirect_stdout, redirect_stderr, daemonize_redirect, java_path, create_new_process_group)
   1977             daemonize_redirect=daemonize_redirect, java_path=java_path,
   1978             create_new_process_group=create_new_process_group)
-> 1979         gateway = JavaGateway(gateway_parameters=GatewayParameters(port=_port))
   1980         return gateway
   1981 

L:\pyzo2015a\lib\site-packages\py4j\java_gateway.py in launch_gateway(port, jarpath, classpath, javaopts, die_on_exit, redirect_stdout, redirect_stderr, daemonize_redirect, java_path, create_new_process_group)
    282     # Launch the server in a subprocess.
    283     classpath = os.pathsep.join((jarpath, classpath))
--> 284     command = [java_path, "-classpath", classpath] + javaopts +     285               ["py4j.GatewayServer"]
    286     if die_on_exit:

TypeError: sequence item 1: expected str instance, NoneType found

我如何在这里解决问题? 谢谢,

2 个答案:

答案 0 :(得分:0)

不确定错误,但以下格式适用于Windows 64位

public partial class CheckoutReview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            NVPAPICaller payPalCaller = new NVPAPICaller();

            string retMsg = "";
            string token = "";
            string PayerID = "";
            NVPCodec decoder = new NVPCodec();
            token = Session["token"].ToString();

            bool ret = payPalCaller.GetCheckoutDetails(token, ref PayerID, ref decoder, ref retMsg);
            if (ret)
            {
                Session["payerId"] = PayerID;

                var myOrder = new Order();
                myOrder.OrderDate = Convert.ToDateTime(decoder["TIMESTAMP"].ToString());
                myOrder.Username = User.Identity.Name;
                myOrder.FirstName = decoder["FIRSTNAME"].ToString();
                myOrder.LastName = decoder["LASTNAME"].ToString();
                myOrder.Address = decoder["SHIPTOSTREET"].ToString();
                myOrder.City = decoder["SHIPTOCITY"].ToString();
                myOrder.State = decoder["SHIPTOSTATE"].ToString();
                myOrder.PostalCode = decoder["SHIPTOZIP"].ToString();
                myOrder.Country = decoder["SHIPTOCOUNTRYCODE"].ToString();
                myOrder.Email = decoder["EMAIL"].ToString();
                myOrder.Total = Convert.ToDecimal(decoder["AMT"].ToString());

                // Verify total payment amount as set on CheckoutStart.aspx.
                try
                {
                    decimal paymentAmountOnCheckout = Convert.ToDecimal(Session["payment_amt"].ToString());
                    decimal paymentAmoutFromPayPal = Convert.ToDecimal(decoder["AMT"].ToString());
                    if (paymentAmountOnCheckout != paymentAmoutFromPayPal)
                    {
                        Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                }

                // Get DB context.
                ProductContext _db = new ProductContext();

                // Add order to DB.
                _db.Orders.Add(myOrder);
                _db.SaveChanges();

                // Get the shopping cart items and process them.
                using (WingtipToys.Logic.ShoppingCartActions usersShoppingCart = new WingtipToys.Logic.ShoppingCartActions())
                {
                    List<CartItem> myOrderList = usersShoppingCart.GetCartItems();

                    // Add OrderDetail information to the DB for each product purchased.
                    for (int i = 0; i < myOrderList.Count; i++)
                    {
                        // Create a new OrderDetail object.
                        var myOrderDetail = new OrderDetail();
                        myOrderDetail.OrderId = myOrder.OrderId;
                        myOrderDetail.Username = User.Identity.Name;
                        myOrderDetail.ProductId = myOrderList[i].ProductId;
                        myOrderDetail.Quantity = myOrderList[i].Quantity;
                        myOrderDetail.UnitPrice = myOrderList[i].Product.UnitPrice;

                        // Add OrderDetail to DB.
                        _db.OrderDetails.Add(myOrderDetail);
                        _db.SaveChanges();
                    }

                    // Set OrderId.
                    Session["currentOrderId"] = myOrder.OrderId;

                    // Display Order information.
                    List<Order> orderList = new List<Order>();
                    orderList.Add(myOrder);
                    ShipInfo.DataSource = orderList;
                    ShipInfo.DataBind();

                    // Display OrderDetails.
                    OrderItemList.DataSource = myOrderList;
                    OrderItemList.DataBind();
                }
            }
            else
            {
                Response.Redirect("CheckoutError.aspx?" + retMsg);
            }
        }
    }

    protected void CheckoutConfirm_Click(object sender, EventArgs e)
    {
        Session["userCheckoutCompleted"] = "true";
        Response.Redirect("~/Checkout/CheckoutComplete.aspx");
    }
}

答案 1 :(得分:0)

问题是由于jaydebeapi使用命令java来执行java jar。不幸的是,我使用的计算机只接受java的绝对路径,所以我需要在调用connect方法jaydebeapi时引用它。 谢谢,