从MS Access中的一个表中随机选择值,然后将其插入到另一个表中

时间:2018-10-29 16:09:28

标签: java sql ms-access

正如标题所述,我正在使用MS Access,但遇到了一些麻烦。

  1. 老实说,我无法弄清楚如何从数据库的20个元素中随机选择5个。我一直在尝试在线找到它,所以我一直试图确保至少可以使用所有信息创建表。如果您想为我提供帮助,我将不胜感激,但我知道人们不喜欢在此类网站上发布代码。我主要只关注下一部分。

  2. 程序运行正常,并且出现抽奖成功的消息,但未在Access中创建表。我的教授似乎认为这与打开数据库时出现的记录锁定文件有关。老实说他不是最好的程序员,但是显然他比我更博学。这可能是问题还是我的代码不正确?

            Connection raffleconnection = null;
            Statement rafflestatement = null;
    
            ResultSet rsTickets = null;
    
    
            // Loading or registering Oracle JDBC driver class
            try {
    
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            }
            catch(ClassNotFoundException cnfex) {
    
                System.out.println("Problem in loading or "
                        + "registering MS Access JDBC driver");
                cnfex.printStackTrace();
            }
    
            // Opening database connection
            try {
    
                String strDatabase = "C:\\Users\\nikol\\OneDrive\\Documents\\Fall Semester 2018\\MIS 413\\AdidasRaffle\\AdidasRaffle.accdb";
                String strURL = "jdbc:ucanaccess://" + strDatabase; 
    
                // Create and get connection using DriverManager class
                raffleconnection = DriverManager.getConnection(strURL); 
    
                // Creating JDBC Statement 
                rafflestatement = raffleconnection.createStatement();
    
                int intTicketNum = 0;
                int intRaffleCustID = 0;
    
                // Executing SQL; retrieve data into ResultSet
                rsTickets = rafflestatement.executeQuery("SELECT * FROM Tickets");
    
                while (rsTickets.next()) {
                    intTicketNum = rsTickets.getInt("TicketID");
                    intRaffleCustID = rsTickets.getInt("CustomerID");
    
    
                }
    
    
                rafflestatement.executeQuery("CREATE TABLE Winners (TicketID INT not NULL, CustomerID INT, PRIMARY KEY (TicketID))");
    
                rafflestatement.executeQuery("INSERT into Winners (TicketID, CustomerID) VALUES('" + intTicketNum + "','" + intRaffleCustID +"')");             
    
            }
    
            catch(SQLException sqlex){
                sqlex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Database Connection Failed");
            }
            finally {
                JOptionPane.showMessageDialog(null, "Raffle Successful");
                // Closing database connection
                try {
                    if(null != raffleconnection) {
    
                        // cleanup resources, once after processing
                        rsTickets.close();
    
                        rafflestatement.close();
    
    
                        // and then finally close connection
                        raffleconnection.close();
                    }
                }
                catch (SQLException sqlex) {
                    sqlex.printStackTrace();
                }
    
            }
    

0 个答案:

没有答案