Java operation access program this is the source code java program to write windows really uncomfortable this code is for a brother don't write program cousin to do the job (his requirements must comply with the standards of cainiao :), so the code is so sorry! However, the key code CodeLoadJDBCdriverClass. forName (sun. jdb
Java operation access program this is the source code java program to write windows really uncomfortable this code is for a brother don't write program cousin to do the job (his requirements must comply with the standards of cainiao :), so the code is so sorry! However, the function is to drop the key Code // LoadJDBCdriver Class. forName ("sun. jdb
Java operation access program this is the source code
Java programs that write windows are really uncomfortable. This code is a job for a cousin of a brother who does not write a program (his requirement must comply with the cainiao standard :......), So the code is so sorry! However, the function is flawed.
Key code
Code
// Load JDBC driver
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
/* The url of the database must be correctly written. This is the key. DBQ can be an absolute or relative path. To reflect the/Independence of the data storage path, you can copy the database to a different bit */
String dbUrl = "jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = access \ test. mdb ";
Connection con = DriverManager. getConnection (dbUrl ,"",
"");
Statement state = con. createStatement (
ResultSet. TYPE_SCROLL_SENSITIVE,
ResultSet. CONCUR_UPDATABLE );
// Execute the SQL statement
String SQL = "select * from score ";
ResultSet rs = state.exe cuteQuery (SQL );
DisplayResultSet (rs );
Rs. close ();
State. close ();
Con. close ();
Code
/*************************************** ************************************
* Display the results retrieved from the database as tables
**************************************** **********************************/
Private void displayResultSet (ResultSet rs) throws SQLException {
Boolean moreRecords = rs. next (); // locate the first record
If (! MoreRecords ){
JOptionPane. showMessageDialog (null, "no record in result set", "No record ",
JOptionPane. INFORMATION_MESSAGE );
Return;
}
Vector rows = new Vector ();
Vector columnHeads = new Vector ();
Try {
ResultSetMetaData rsmd = rs. getMetaData (); // obtain the attribute information of the column in the rs result set.
For (int I = 1; I <= rsmd. getColumnCount (); ++ I)
ColumnHeads. addElement (rsmd. getColumnName (I); // obtain the column name (store the column name to the vector columnHeads)
Do {
Rows. addElement (getNextRow (rs, rsmd ));
}
While (rs. next (); // use a loop to obtain all records
JTable = new JTable (rows, columnHeads); // reconstructs the table view by using the row and column data as parameters.
JTable. setSize (new Dimension (383, 81 ));
JScrollPane scroller = new JScrollPane (jTable); // create a panel with a scroll bar and add the table View
Container c = getContentPane (); // get the Solver
// C. remove (2 );//
// Remove the specified control from the solver (there are two level-2 panels in this form, the first is to store the text field and button, and the second is to store the table view, so remove 1)
C. add (scroller, BorderLayout. CENTER); // add the panel to the solver.
C. validate (); // verify the container and all its child components
} Catch (SQLException e ){
E. printStackTrace ();
}
}
/**
* Remove a row
*/
Private Vector getNextRow (ResultSet rs, ResultSetMetaData rsmd)
Throws SQLException {
Vector currentRow = new Vector (); // defines a Vector for storing records
For (int I = 1; I <= rsmd. getColumnCount (); ++ I)
CurrentRow. addElement (rs. getString (I); // obtain the record
Return currentRow; // return record
}
The above code connects to access and displays the retrieved data on the JTable control ......
Select JTable row: jTable. getSelectedRow ()! =-1 is used to determine whether a selected row exists. (jTable. getModel (). getValueAt (jTable
. GetSelectedRow (), 0). toString (); used to retrieve a unit value of the selected row (0 indicates the first column of the row and so on)
If (jTable. getSelectedRow ()! =-1 ){
String ID = (jTable. getModel (). getValueAt (jTable
. GetSelectedRow (), 0). toString ();
Used to insert acces data
"Insert into score (name, language, math, english) values (......) "A table name marked in red must be followed by a space. Otherwise, it cannot be inserted!