Basic query and update operations on a database using Java _java

Source: Internet
Author: User
Tags odbc prev

Database query

The statement object is established by using the Createstatement method of the Connection object, the ExecuteQuery () method of the statement object is used to query the SQL query, and the result set is returned, and then it is shaped like getxxx (). method to read data from the result set. This series of steps will enable you to query the database.

The example Java application accesses the database. The application opens the Examinee information table Ksinfo, from which the examinee's information is taken out. 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.printstacktr
      Ace (); return null;
    Failed to load, connection unsuccessful} try{con = drivermanager.getconnection (URL, username, password);
      catch (SQLException e) {e.printstacktrace (); return null; Connection failed} return con;
  Connection succeeded} String title[] ={"test number", "name", "Score", "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 one");
  JButton next = new JButton ("latter"); JButton First = new JButton (the "one");
  JButton last = new JButton ("final"); Statement SQL; SQL Statement Object ResultSet RS;
    Store query Result Object Example10_9 (Connection Connect) {super ("Examinee 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 (a);
      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 test
    Sheng Test number "," Input Test 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 (a);
      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 ("Test Number")) Txtname.settext (rs.getstring ("name")) Txtscore.
      SetText (rs.getstring ("achievement")); 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 () = = i Rs.first (); else if (e.getsource () = last) rs.last ();
    Readrecord ();
    The 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
Database update operations include data table creation, deletion, and data table records additions, deletions, modifications, and so on. If the data SQL command is implemented, the SQL UPDATE statement is executed using the statement executeupdate () method, the data table is modified, and the SQL INSERT statement is implemented to add the data table records.

For example, in the previous data for the query example based on, and then add to the data table modification and insertion. Limited to space, no longer give a complete program, only to implement the method of modification and insertion. The program can be added to insert,,, in addition to save the button, through the existing browsing, positioning to a specific location of the datasheet, the 痈疽 record for editing, or insert, or delete, and then press the Save button, complete the modified data table to save.

The following code explains how the datasheet is updated. When you connect to a datasheet, you specify that the obtained ResultSet object is updatable.

  stmt = Connect.createstatement (resultset.type_scroll_insensitive,resultset.concur_updatable);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.