vocationcondb.java~5~

来自「用java实现的一个应用程序,源码非常完整,可以直接运行」· JAVA~5~ 代码 · 共 107 行

JAVA~5~
107
字号
package 毕业设计;
import java.sql.*;
import javax.swing.JOptionPane;
import java.util.Vector;
public class VocationConDB {
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private PreparedStatement pst;
    public VocationConDB() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Driver 出错");
        }
        try {
            String url = "jdbc:odbc:chenhaiLibrary";
            con = DriverManager.getConnection(url);
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                     ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex1) {
            System.out.println("lib 出错");
        }
    }

    public boolean AddVocation(String Vocation) {
        boolean Success = true;
        try {
            String strSQL = "insert Vocation values ( '" + Vocation + "')";
            pst = con.prepareStatement(strSQL);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
            Success = false;
        }
        return Success;
    }

    public Vector SearchAll() {
        Vector vt = new Vector();
        try {
            String str = "select * from Vocation";
            rs = st.executeQuery(str);
            while (rs.next()) {
                Vector temp = new Vector();
                temp.add(rs.getString(1));
                temp.add(rs.getString(2));
                vt.add(temp);
            }
            rs.close();
        } catch (SQLException ex) {
        }
        return vt;
    }

    public Vector SearchVocation(String Vocation) {
        Vector vt = new Vector();
        try {
            String str = "select * from Vocation where VocationName like '%" +
                         Vocation + "%'";
            rs = st.executeQuery(str);
            while (rs.next()) {
                Vector temp = new Vector();
                temp.add(rs.getString(1));
                temp.add(rs.getString(2));
                vt.add(temp);
            }
            rs.close();
        } catch (SQLException ex) {
        }
        return vt;
    }

    public boolean UpdateVocation(int Id, String VocationName) {
        boolean Success = true;
        try {
            String strSQL = "update Vocation set VocationName = '" +
                            VocationName +
                            "' where Id = " + Id;
            pst = con.prepareStatement(strSQL);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
            Success = false;
        }
        return Success;
    }

    public void DeleteVocation(int Id) {
        try {
            String strSQL = "delete from Vocation where Id = " + Id;
            pst = con.prepareStatement(strSQL);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
        }
    }

    public void CloseVocationDB() {
        try {
            st.close();
            con.close();
        } catch (SQLException ex) {
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?