A Java tool class that connects to a database with a configuration file

Source: Internet
Author: User
Tags getmessage odbc stmt

When you use it, you just have to change the contents of the configuration file.

Configuration file Database.property Content

jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.url=jdbc:odbc:mydata
jdbc.username=system
jdbc.password=manager

JavaBean File Contents

package community.database;


import java.io.*;


import java.util.*;


import java.sql.*;


/** *//**


*


title:database Manager


*


Description:connect Database


*


Copyright:copyright 2002 Yaoguodong


*


Company:


* @author unascribed yaoguodong Yiu Dong


* @version 1.0


* @email net_spirit@163.com


* @QQ 16898283


* Please keep the copyright information when you reprint it


*/


public class Dbmanager ... {


/** *//**getconnection () Gets the contents of the properties file of the connected database and returns the connection result set


@throws SQLException


@throws IOException


@throws Exception


declare drivers, get the driver for the database


the URL to connect to the remote database


declare username, get the username to connect to the remote database


declares password, gets the password to connect to the remote database


@return Drivermanger.getconnection (URL,USERNAME,PASSOWRD);


 */


public Connection getconnection () throws sqlexception,ioexception,exception ... {


Properties Props = new properties ();


//string fileName = "E:\Database.Property";


//fileinputstream in = new FileInputStream (fileName);


InputStream in = GetClass (). getResourceAsStream ("/database.property");


props.load (in);


String drivers = Props.getproperty ("Jdbc.drivers");


if (drivers!=null)


    ... {System.setproperty ("jdbc.drives", drivers);}


String url = props.getproperty ("Jdbc.url");


String username = props.getproperty ("Jdbc.username");


String password = props.getproperty ("Jdbc.password");


class.forname (drivers);


return drivermanager.getconnection (Url,username,password);


}


/** *//**dbmanager ()


* Connection Database


 */


public Dbmanager () ... {


Try ... {


conn = getconnection ();


stmt=conn.createstatement ();


System.out.println ("Connect Database is ok!");


  }


catch (Exception e) ... {


System.out.println ("Connect Database failed!:" +e);


   }


}


/** *//** executequery () method, query SQL results


@param sqlwords The SQL statement for the incoming query


@return RS for the returned query result set


 */


public ResultSet executequery (String sqlwords) ... {


This.sqlwords=sqlwords;


Try ... {


rs=stmt.executequery (sqlwords);


  }


catch (SQLException ex) ... {


System.out.println ("Execute Query Sql failed!:" + ex.getmessage ());


  }


return RS;


}


/** *//** executeupdate () method, modify database records


@param sqlwords The SQL for the incoming query


@return True|false


 */


public boolean executeupdate (String sqlwords) ... {


this.sqlwords=sqlwords;


Try ... {


stmt.executeupdate (sqlwords);


return true;


  }


catch (SQLException ex) ... {


System.err.println ("Execute Update Sql failed!:" + ex.getmessage ());


return false;


  }


}


/** *//** Executeinsert () method, insert a new database record


@param sqlwords SQL for incoming inserts


@return True|false


 */


public boolean Executeinsert (String sqlwords) ... {


this.sqlwords=sqlwords;


Try ... {


stmt.executeupdate (sqlwords);


return true;


  }


catch (SQLException ex) ... {


System.err.println ("Execute Insert Sql failed!:" + ex.getmessage ());


return false;


  }


}


/** *//** Executedelete () method, deleting database records


@param sqlwords for incoming deletion of SQL


@return True|false


 */


public boolean executedelete (String sqlwords) ... {


this.sqlwords=sqlwords;


Try ... {


stmt.executeupdate (sqlwords);


return true;


  }


catch (SQLException ex) ... {


System.err.println ("Execute Delete Sql failed!:" + ex.getmessage ());


return false;


  }


}


/** *//** Close () method to disconnect the database


@return True|false


 */


public boolean close ()


... {


Try ... {


if (rs!= null) ... {Rs.close ();}


if (stmt!= null) ... {Stmt.close ();}


if (conn!= null) ... {Conn.close ();}


return true;


  }


catch (Exception e) ... {


System.out.print ("Clost Database Connect failed!:" +e);


return false;


  }


}


/** *//**


* Declaration Conn


* Declaration RS result set


* Statement stmt Tone


* Declaration sqlwords Keyword


 */


Connection conn = null;


ResultSet rs = null;


Statement stmt = null;


private String sqlwords = null;

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.