Salve, ho un dubbio per quanto riguarda l'inserimento di una query su una tabella.
Questo è quello che ho trovato sulla guida oracle. Ora ho esteso il seguente codice con il seguente metodocodice:private String[] columnNames = {"First Name", "Last Name"}; private Object[][] data = { {"Kathy", "Smith"} }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * JTable uses this method to determine the default renderer/ * editor for each cell. If we didn't implement this method, * then the last column would contain text ("true"/"false"), * rather than a check box. */ public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. return true; } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; fireTableCellUpdated(row, col); if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + data[i][j]); } System.out.println(); } System.out.println("--------------------------"); } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("TableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. Prova20 newContentPane = new Prova20(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Ora voglio sapere come passare il contenuto di datafinal sulla tabella.codice:public String insertable(String Nometabella){ Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root",""); //STEP 4: Execute a query System.out.println("Creating database..."); stmt = conn.createStatement(); String sql = "USE STUDENTS;"; stmt.executeUpdate(sql); String queryfinal="SELECT* FROM "; String querytabella=queryfinal.concat(Nometabella).concat(";"); System.out.println(""+querytabella); ResultSet rs = stmt.executeQuery(querytabella); Vector<String> temp = new Vector<>(); Prova20 newdata=new Prova20(); while (rs.next()) { Nome2=rs.getString("Nome"); Cognome2=rs.getString("Cognome"); temp.addElement(Nome2); Object [][]data = { {Nome2},{Cognome2} }; datafinal=data; System.out.println("oggetto"+newdata.datafinal); //Create and set up the window. } System.out.println("Database created successfully..."); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); return Nometabella; }
il fatto e che sostituendo a posto di data datafinal mi dà errore.

					
					
					
						
  Rispondi quotando
 ).