행위

Python jdbc 테이블

DB CAFE

# -*- coding: utf-8 -*-
import os
import sys

import jaydebeapi
import jpype
import jaydebeapi as jp
import pandas.io.sql as pd_sql
from pandas import DataFrame
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def loadData(self):

        jHome = jpype.getDefaultJVMPath()
        # print(jHome)
        #
        jpype.startJVM(jHome,'-Djava.class.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/ojdbc6.jar','locale=UTF-8')
        conn = jp.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:username/password@db_ip:port_1521:sid_name')
        curs = conn.cursor()
        sql = "select * from employees"
        # self.creatingTables()
        curs.execute(sql)

        self.tableWidget.setRowCount(0)
        self.tableWidget.insertRow(int(curs.rowcount))
        row_data = curs.fetchall()

        # self.tableWidget.setItem(1, 1, QtWidgets.QTableWidgetItem(str(row_data[1])))
        # self.tableWidget = QTableWidget()
        # self.tableWidget.setRowCount(5)
        self.tableWidget.setColumnCount(10)

        for row_number,row_data in enumerate(row_data):
            print('rownumber:',row_number)
            self.tableWidget.insertRow(row_number)
            for column_number,data in enumerate(row_data):
                print('column_number:', column_number)
                print('data:', data)
                self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))

        curs.close()
        conn.close()
        jpype.shutdownJVM()


    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(80, 30, 581, 341))
        self.tableWidget.setRowCount(5)
        self.tableWidget.setColumnCount(10)
        self.tableWidget.setObjectName("tableWidget")

        self.btn_load = QtWidgets.QPushButton(self.centralwidget)
        self.btn_load.setGeometry(QtCore.QRect(310, 420, 113, 32))
        self.btn_load.setObjectName("btn_load")
        self.btn_load.clicked.connect(self.loadData)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btn_load.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())