JDBC package and JAVA standard data processing, access to MS SQL SERVER

JDBC package and JAVA standard data processing, access to MS SQL SERVER, at the end of the article there are JAVA source code download and MS SQL SERVER example database to download the recoverable file

Environment: jdk-14.0.1_windows-x64 MS SQLSERVER2014

//Package

//JDBC package class//

package jdbcutils;


    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    //import java.lang.reflect.Field;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    //import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    //import java.util.ArrayList;
    //import java.util.HashMap;
    import java.util.List;
//import java.util.Map;
// import java.utiljdbcutils .Map;
    import java.util.Properties;

    public class JdbcUtils
    {          // Indicates that the user name of the database is defined         private final String USERNAME = "sa";         // The password of the database is defined         private final String PASSWORD = "3201319";         // The driver information of the database is defined         private final String DRIVER = "com .microsoft.sqlserver.jdbc.SQLServerDriver";         // Define the address to access the database         private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bhjs";         // Define the link to the database         private Connection connection;         // Define the execution object of the SQL statement         private PreparedStatement pstmt;         // Define the result set returned by the query         private ResultSet resultSet;













        public void JdbcUtils()
        {
            try
            {
                Class.forName(DRIVER);
                System.out.println("注册驱动成功!!");
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
        }

        // Define the connection to the database
        public Connection getConnection()
        {             try             {                 connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);                 System.out.println("Database connection is successful");             }             catch (Exception e)             {                 // TODO: handle exception             }             return connection;         }         public ResultSet findMoreResult(String sql, List params) throws SQLException //The query returns multiple rows of values         {             int index = 1;             ResultSet resultSet;











        




            pstmt = connection.prepareStatement(sql);
            if (params != null && !params.isEmpty())
            {
                for (int i = 0; i < params.size(); i++)
                {
                    pstmt.setObject(index++, params.get(i));
                }
            }
            resultSet = pstmt.executeQuery();
                     
            return resultSet;
        }

        public boolean updateByPreparedStatement(String sql, List params) throws SQLException //Update
                
        {             boolean flag = false;             int result = -1;// Indicates the number of rows in the database affected when the user performs addition, deletion and modification             pstmt = connection.prepareStatement (sql);             int index = 1;             // Fill the placeholders in the SQL statement             if (params != null && !params.isEmpty())             {                 for (int i = 0; i <params.size(); i++ )                 {                     pstmt.setObject(index++, params.get(i));                 }             }             result = pstmt.executeUpdate();             flag = result> 0? true: false;














             return flag;
        }
        
        public void releaseConn()  //释放连接
        {
            if (resultSet != null)
            {
                try
                {
                    resultSet.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (pstmt != null)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

 

    }

//Test application/

package form1;

import java.awt.Component;
import java.awt.EventQueue;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

// import java.util.Vector;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollBar;
import java.awt.ScrollPane;
import javax.swing.JTextField;
//import javax.swing.table.DefaultTableModel;
// import javax.swing.ListSelectionModel;
import jdbcutils.JdbcUtils;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Panel;
import java.awt.Color;

 

public class form2 {

    private JFrame frmJdbc;
    
    

    //private static final String JdbcUtils1 = null;
    private JTable table;
    JScrollPane jScrollPane = null;
    private JTextField textField;
    private JTextField textField_1;

    

    
    
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    form2 window = new form2();
                    window.frmJdbc.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public form2() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmJdbc = new JFrame();
        frmJdbc.setTitle("JDBC\u5C01\u88C5\u548C\u6570\u636E\u5904\u7406");
        
        frmJdbc.setBounds(400, 100, 636, 561);
        
        
        
        
        
        frmJdbc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmJdbc.getContentPane().setLayout(null);
        
        
        
        ///
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet res = null;
        List parm1=null;
        try {
            
            JdbcUtils JdbcUtils1 =new JdbcUtils();
            JdbcUtils1.JdbcUtils();
            con=JdbcUtils1.getConnection();
            
            String sql = "select * from info_jqmc ";//查询test表
            res=JdbcUtils1.findMoreResult(sql,parm1);  //调用jdbc封装类中  查询函数
                                      
            Vector line1;
            Vector rows,columnNames;
            rows = new Vector();
            
            columnNames = new Vector();
            columnNames.add("jqmc");
            columnNames.add("jqip");
            
            
            while(res.next()){
                             
                               line1 = new Vector();
                               line1.add(res.getString("jqmc"));
                               line1.add(res.getString("jqip"));
                               
                               rows.add(line1);
                             }
          //定义JTable的对象
         
           
            
             
            table = new JTable(rows, columnNames);
            table.setBounds(41, 37, 467, 306);
            
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int selectRows=table.getSelectedRows().length;// 取得用户所选行的行数
                    
                    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

                    
                      int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行
                      textField.setText((String)table.getValueAt(selectedRowIndex,0));
                      textField_1.setText((String)table.getValueAt(selectedRowIndex,1));
                      // 进行相关处理
                
                    
                }
            });
            
            jScrollPane = new JScrollPane(table);
            jScrollPane.setToolTipText("");
            
            
            jScrollPane.setBounds(43, 23, 534, 308);
            frmJdbc.getContentPane().add(jScrollPane);
            
        } catch (Exception e1) {
            // TODO: handle exception
            
            e1.printStackTrace();
           
        }finally{
            try {
                if(res != null) res.close();
                if(statement != null) statement.close();
                if(con != null) con.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        /
        
        
        
        JButton btnNewButton = new JButton("\u67E5\u8BE2");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
                
            }
            
            
            
            
        });
        btnNewButton.setBounds(25, 430, 93, 23);
        frmJdbc.getContentPane().add(btnNewButton);
        
        JButton btnNewButton_1 = new JButton("\u9000\u51FA");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //lblNewLabel.setText("加载驱动成功!");
            }
        });
        btnNewButton_1.setBounds(497, 430, 93, 23);
        frmJdbc.getContentPane().add(btnNewButton_1);
        
        JButton btnNewButton_2 = new JButton("\u5220\u9664");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {   //删除
                Connection con = null;
                //List param2=null;
                boolean fl;
                
                int selectRows=table.getSelectedRows().length;// Get the number of rows selected by the user
                DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

                if(selectRows==1){                   int selectedRowIndex = table.getSelectedRow(); // Get a single row                   JdbcUtils selected by the user JdbcUtils2 =new JdbcUtils();                   JdbcUtils2.JdbcUtils();                   con=JdbcUtils2.getConnection();                   // Perform correlation Processing                   String sql1 = "DELETE from info_jqmc WHERE jqmc=?"; //Update the test table                   List param2 = new ArrayList();                   param2.add(textField.getText());                   try                       {                       fl=JdbcUtils2.updateByPreparedStatement(sql1, param2) ;                       }                       catch (Exception e1)

                





                  

                

                





                      {                     e1.printStackTrace();                       }                 }                 else                 {                     JOptionPane.showMessageDialog(null,"Please select the row first, and then click to delete");                     }






            
            }
        });
        btnNewButton_2.setBounds(261, 430, 93, 23);
        frmJdbc.getContentPane().add(btnNewButton_2);
        
        JButton btnNewButton_3 = new JButton("\u4FDD\u5B58");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Connection con = null;
                //List param2=null;
                boolean  fl;
                
                int selectRows=table.getSelectedRows().length;// 取得用户所选行的行数
                DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

                if(selectRows==1){                   int selectedRowIndex = table.getSelectedRow(); // Get a single row                   JdbcUtils selected by the user JdbcUtils2 =new JdbcUtils();                   JdbcUtils2.JdbcUtils();                   con=JdbcUtils2.getConnection();                   // Perform correlation Processing                   String sql1 = "UPDATE info_jqmc SET jqmc=?,jqip=? WHERE jqmc=?"; //Update the test table                   List param2 = new ArrayList();                   param2.add(textField.getText());                   param2.add(textField_1 .getText());                   param2.add(textField.getText());                   try                       {

                





                  

                





                      fl=JdbcUtils2.updateByPreparedStatement(sql1, param2);
                      }
                      catch (Exception e1)
                      {
                    e1.printStackTrace();
                      }
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"请先选中行,再点击保存");    
                }
            }
        });
        btnNewButton_3.setBounds(379, 430, 93, 23);
        frmJdbc.getContentPane().add(btnNewButton_3);
        
        JLabel lblNewLabel_1 = new JLabel("\u673A\u5668\u540D\u79F0\uFF1A");
        lblNewLabel_1.setBounds(54, 385, 60, 15);
        frmJdbc.getContentPane().add(lblNewLabel_1);
        
        textField = new JTextField();
        textField.setBounds(132, 382, 96, 21);
        frmJdbc.getContentPane().add(textField);
        textField.setColumns(10);
        
        JLabel lblNewLabel_2 = new JLabel("\u673A\u5668\u72B6\u6001");
        lblNewLabel_2.setBounds(238, 385, 55, 15);
        frmJdbc.getContentPane().add(lblNewLabel_2);
        
        textField_1 = new JTextField();
        textField_1.setBounds(303, 382, 96, 21);
        frmJdbc.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        
        JLabel lblNewLabel = new JLabel("\u7F16\u8F91\uFF1A");
        lblNewLabel.setBounds(54, 360, 54, 15);
        frmJdbc.getContentPane().add(lblNewLabel);
        
        JButton btnNewButton_4 = new JButton("\u65B0\u589E");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_4.setBounds(143, 430, 93, 23);
        frmJdbc.getContentPane().add(btnNewButton_4);
        
        //table_1 = new JTable();
        //table_1.setBounds(63, 87, 300, 141);
        //frame.getContentPane().add(table_1);
        
        
        
        
        
        
        
        
    }
}


Source code download: link: https://pan.baidu.com/s/1SAhG8whvK99_64jdlIA5zA Extraction code: g83s

Example: MS SQL SERVER database recoverable file download:

 Link: https://pan.baidu.com/s/11NdjOptjs_EZRfcr7UHk5A Extraction code: s88h

JDBC下载:Microsoft JDBC Driver 7.0 for SQL Server

Link: https://pan.baidu.com/s/1PonMpXUKEQyQu3Nf-mtoTw Extraction code: w5ys

jdk-14.0.1_windows-x64_bin

Link: https://pan.baidu.com/s/1_Csf5zKneP0i-yYAr3TcIg Extraction code: 2f62

eclipse-java-2020-03-R-win32-x86_64

Link: https://pan.baidu.com/s/1BDKwzni6-S05hLS9T_5DLg Extraction code: 3qs2

windowbuilder java interface design package

Link: https://pan.baidu.com/s/1gfsv4ozdQ2Nz4FRKx_xwEg Extraction code: er5n

 

 

Guess you like

Origin blog.csdn.net/fanxiaoduo1/article/details/106218862