Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Connection conn = null;
- private Statement st=null;
- private ResultSet rs=null;
- public void setConnection(){
- host="localhost";
- database="test";
- username="testtest";
- password="testtest";
- String url = "jdbc:mysql://" + host + "/" +database+ "";
- try {
- Class.forName ("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection (url, username, password);
- System.out.println("Succesfuly connected to the database!");
- } catch(Exception e){
- System.out.println("Failed to connect to the database.");
- e.printStackTrace();
- }
- }
- private boolean checkConnection(){
- try {
- ResultSet cc = st.executeQuery("SELECT 1");
- return true;
- } catch (SQLException e) {
- int n = JOptionPane.showConfirmDialog(
- frame,
- "The connection was closed, reconnect?",
- "Database security measure",
- JOptionPane.YES_NO_OPTION);
- if(n == JOptionPane.YES_OPTION){
- try {
- conn.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- setConnection();
- } else if ( n == JOptionPane.NO_OPTION){
- frame.setVisible(false);
- new LoginWindowB();
- }
- return false;
- }
- }
- btn1.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- if(checkConnection()){
- try {
- rs = st.executeQuery("SELECT * FROM " + selectedTable);
- loadData(rs);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement