Database query
Use the createStatement method of the Connection object to create a Statement object, use the executeQuery() method of the Statement object to execute the SQL query statement and return the result set, and then read the data from the result set using a method such as getXXX(). After such a series of steps, the query to the database can be realized.
[Example] Java application accesses the database. The application opens the candidate information table ksInfo and retrieves various information about the candidate. Assume that the structure of the candidate information database is as follows:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.sql.*;public class Example10_9 extends JFrame implements ActionListener{ public static Connection connectByJdbcodbc( String url, String username,String password){ Connection con = null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load ODBC driver} catch (Exception e){ e.printStackTrace(); return null; //Loading failed, connection was unsuccessful} try{ con = DriverManager.getConnection(url, username, password); } catch (SQLException e){ e.printStackTrace(); return null; //Connection failed} return con; //Connection successful} String title[] ={"Examination number", "Name", "Grade", "Address", "Resume"}; JTextField txtNo = new JTextField(8); JTextField txtName = new JTextField(10); JTextField txtScore = new JTextField(3); JTextField txtAddr = new JTextField(30); JTextArea txtresume = new JTextArea(); JButton prev = new JButton("previous"); JButton next = new JButton("next"); JButton first = new JButton("first"); JButton last = new JButton ("last"); Statement sql; //SQL statement object ResultSet rs; //Storage query result objectExample10_9(Connection connect){ super("Candidate information viewing window"); setSize(450, 350); try{ sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = sql. executeQuery("SELECT * FROM ksInfo"); Container con = getContentPane(); con.setLayout(new BorderLayout(0, 6); JPanel p[] = new JPanel[4]; for (int i = 0; i < 4; i++){ p[ i] = new JPane(new FlowLayout(FlowLayout.LEFT, 8, 0)); p[i].add(new JLabel(title[i])); } p[0].add(txtNo); p[1].add(txtName); p[2].add(txtScore); p[3].add(txtAddr); JPanel p1 = new JPane(new GridLayout94, 1, 0, 8)); JScrollPane jsp = new JScrollPane(txtResume, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jsp.setPreforredSize(new Dimension(300, 60); for (int i = 0; i < 4; i++){ p1.add(p[i]); } JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0); p2.add(new JLabel(title[4])); p2.add(jsp); Jpanel p3 = new Jpanel(); p3.add(prev); p3.add(next); p3.add(first); p3.add(last); prev.addActionListener(this); next.addActionListener(this); first.addActionListener(this); last.addActionlistener(this); rs.first(); readRecord(); } catch (Exception e){ e.printStackTrace(): } setVisible(ture); } public void modifyRecord(Connection connect){ String stuNo = (String)JOptionPane.showInputDialog(null, "Please enter the candidate's exam number", "Enter the exam number dialog box", JOptionPane.PLAIN_MESSAGE, null, null, ""); try { sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = sql.executeQuery ("SELECT * FROM ksInfo"); Container con = getContentPane(); con.setLayout(new Boarderlayout(0, 6)); Jpanel p[] = new JPanel[4]; for (int i = 0; i < ; i++){ p[i] = new JPane(new FlowLayout(flowLayout.LEFT, 8, 0)); p[i].add(new JLabel(title[i])); } p[0].add(txtNo); p[1].add(txtName); p[2].add(txtScore); p[3].add(txtAddr); Jpanel p1 = new Jpane(new GridLayout(4 , 1, 0, 8)); JScrollPane jsp = new JScrollPane(txtResume, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jsp.setPreferredSize (new dimension(300, 60)); for (int i = 0; i < 4; i++){ p1.add(p[i]); } Jpanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0)); p2.add(new JLableI(title[4])); p2.add(jsp); JPanel p3 = new JPanel(); p3.add(prev); p3. add(next); p3.add(first); p3.add(last); prev.addActionListener(this); next.addActionListener(this); first.addActionListenerIthis); last.addActionListener(this); rs.first(); readRecord(); } catch (Exception e){ e.printStackTrace(); } setVisible(true); } boolean readRecord(){ try{ txtNo.setText(rs.getString("Examination Number")); txtName.setText(rs.getString("name")); txtScore.setText(rs.getString("score")); txtAddr.setText(rs.getString("address")); txtResume.setText(rs.getString( "Resume")); } catch (SQLException e){ e.printStackTrace(); return false; } return true; } public void actionPerformed(ActionEvent e){ try{ if (e.getSource() == prev)rs.previous(); else if (e.getSource() == next)rs.next(); else if (e.getSource() == first)rs.first(); else if (e.getSource() == last)rs.last(); readRecord(); } catch (Exception e2){} } public static void main(String args[]){ connection connect = null; JFrame .setDefaultLookAndFeeDecorated(true); Font font = new Font("JFrame", Font.PLAIN, 14); if ((connect =connectByJdbcOdbc("jdbc:odbc :redsun", "xia", "1234")) == null){ JOptionPane.showMessageDialog(null, "Database connection failed!"); System.exit ( - 1); } new Example10_9(connect); //Create object}}
Java Database Update <br />Database update operations include data table creation, deletion, and addition, deletion, and modification of data table records. If it is implemented using data SQL commands, use the executeUpdate() method of the Statement pair to execute the SQL update statement to modify the data table; execute the SQL insert statement to add the data table records.
For example, based on the previous data query example, add modification and insertion to the data table. Due to space limitations, the complete program will not be given, only the methods to implement modification and insertion. The program can add insert,,, and save buttons. Through the existing browsing, locate a specific position of the data table, edit, modify, insert, or delete the carbuncle record, and then press the save button to complete the modified data table. save.
The following code explains how to update the data table. When connecting to a data table, you need to specify that the ResultSet object obtained is updatable.
stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);