SQLite Basic (Instance FileOrganizer2013.5.12)

Source: Internet
Author: User
Tags gettext


Tool with SQLite Dev data type: 1.NULL: null value. 2.INTEGER: A signed integer, depending on the size of the range in which the number is deposited. 3.REAL: Floating point number, stored as 8-byte IEEE floating-point numbers. 4.TEXT: string literal. 5.BLOB: Binary object. =======================Add Sqlite-jdbc-3.7.2.jar Connection
import org.sqlite.JDBC;

    Connection conn = null;
    Statement stat;
    ResultSet rs;


// Connect to the database
    void connect () {
        try {
            // JDBC connected to SQLite
            Class.forName ("org.sqlite.JDBC");
            // Establish a connection with the database name zieckey.db.
            conn = DriverManager.getConnection ("jdbc: sqlite: test.db");
            stat = conn.createStatement ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        //JOptionPane.showMessageDialog(null,"Connected database ");
        if (null == conn) {
            JOptionPane.showMessageDialog (null, "Failed to connect to the database");
            System.exit (1);
        }
            
    }

    // Disconnect
    void disconnect () {
        try {
            // rs.close ();
            conn.close (); // End the database connection
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        //JOptionPane.showMessageDialog(null, "Disconnected database");
    } 


Reading meta data and data


private void showMetaData () {// Show the data table header to test the database connection
        ResultSetMetaData rsmd; // Get metadata
        try {
            rs = stat.executeQuery ("Select * From test");
            rsmd = rs.getMetaData ();
            int ColumnCount = rsmd.getColumnCount ();
            for (int j = 1; j <= ColumnCount; j ++) {
                System.out.println (rsmd.getColumnName (j));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        
    }

    private void showDataInTable () {// show data
        // define the data model of the table
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel ();
        try {
            while (rs.next ()) {
                Vector newRow = new Vector ();
                int c = 1, numberOfColumns;
                numberOfColumns = rs.getMetaData (). getColumnCount ();
                while (c <= numberOfColumns) {
                    newRow.addElement (rs.getString (c));
                    c ++;
                }
                dtm.addRow (newRow);
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }


Querying a specified row


SELECT * from MAIN. [Test] where rowid = 10;


Delete


String updateSql="delete from test where RecID =‘"+(String)jTable1.getValueAt(jTable1.getSelectedRow(),0)+"‘"; try {
            stat.executeUpdate(updateSql);
            showDataInTable();
            System.out.println("已delete");
        } catch (SQLException ex) {
            ex.printStackTrace();
        } 


Update


 
int row = jTable1.getSelectedRow();
         String updateSql="update test "// + "set Dept=‘sorry2" + "set Dept=‘"+jTextField2.getText() + "‘, SendID=‘"+jTextField3.getText() + "‘, Title=‘"+jTextField4.getText() + "‘, RecDate=‘"+jTextField5.getText() + "‘, Link=‘"+jTextField6.getText() + "‘ where RecID =‘"+(String)jTable1.getValueAt(row,0)+"‘"; try {
            stat.executeUpdate(updateSql);
            showDataInTable();
            System.out.println("已save");
        } catch (SQLException ex) {
            ex.printStackTrace();
        } 


Insert


String updateSql="insert into test VALUES("
                 + "‘"+jTextField1.getText() + "‘,‘"+jTextField2.getText() + "‘,‘"+jTextField3.getText() + "‘,‘"+jTextField4.getText() + "‘,‘"+jTextField5.getText() + "‘,‘"+jTextField6.getText() +"‘)"; try {
            stat.executeUpdate(updateSql);
            showDataInTable();
}




Related Article

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.