Pure Java way to connect to database simple operation

Source: Internet
Author: User

JDBC Full name Java Datebese connection--java database connection

Package Com.beiwo;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import   Java.sql.sqlexception;import java.sql.statement;import java.util.resourcebundle;/* Database helper Classes */public class DBUtils {   private static String Driverclass;   private static String URL;   private static String user; private static String Password;static{resourcebundle rb=resourcebundle.getbundle ("jdbc");d riverclass=rb.getstring ( "Driverclass"); url=rb.getstring ("url"), user=rb.getstring ("user");p assword=rb.getstring ("password"); try {// Load driver Class.forName (driverclass);} catch (Exception e) {e.printstacktrace ();}} public static Connection Getconn () throws Sqlexception{return drivermanager.getconnection (URL, user, password);} public static void CloseAll (ResultSet rs,statement stme,connection conn) {if (Null!=rs) {try {rs.close ();} catch (Sqlexcep tion e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (null!=rs) {try {stme.close ()} catch (SQLException e) {//TODO auto-generated CATch blocke.printstacktrace ();}} if (null!=rs) {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
Package Com.beiwo;import Java.sql.connection;import Java.sql.resultset;import Java.sql.statement;public class      Mastercurd {public static void FindAll () {Connection conn=null;      Statement Stme=null;      ResultSet Rs=null;  String sql= "SELECT * from Master"; Load Drive try {//class.forname ("com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Load succeeded"); Conn=dbutils.getconn (); stme= conn.createstatement (); rs= stme.executequery (SQL); while ( Rs.next ()) {System.out.println (Rs.getobject (1)); System.out.println (Rs.getobject (2)); System.out.println (Rs.getobject (3)); System.out.println (Rs.getobject (4)); System.out.println ("=============================");}} catch (Exception e) {}finally{dbutils.closeall (RS, STME, conn);}}    Insert public static void Insert () {Connection conn=null;  Statement Stme=null;   String sql= "INSERT into master (name, password, money) VALUES (' Zhang Zheng ', ' 123 ', 100)"; try {//Get Connection Object conn= Dbutils.getconn (); SYSTEM.OUT.PRINTLN ("Connection succeeded"); Gets the Statement object Stme=conn.createstatement ();//Executes the statement Stme.execute (SQL);} catch (Exception e) {e.printstacktrace ();} Finally{dbutils.closeall (NULL, STME, conn);}}    Update public static void update () {Connection conn=null;    Statement Stme=null;   String sql= "UPDATE master SET name= ' Zhang June ', money=50 where id=6"; try {//Get Connection Object conn= Dbutils.getconn (); SYSTEM.OUT.PRINTLN ("Connection succeeded"); Get Statement Object Stme=conn.createstatement ();//EXECUTE statement stme.executeupdate (SQL);} catch (Exception e) {e.printstacktrace ();} Finally{dbutils.closeall (NULL, STME, conn);}}    Delete public static void Delete () {Connection conn=null;    Statement Stme=null;   String sql= "DELETE from master WHERE id=7"; try {//Get Connection Object conn= Dbutils.getconn (); SYSTEM.OUT.PRINTLN ("Connection succeeded"); Get Statement Object Stme=conn.createstatement ();//EXECUTE statement stme.executeupdate (SQL);} catch (Exception e) {e.printstacktrace ();} Finally{dbutils.closeall (NULL, STME, conn);}}}

Package Com.beiwo;/*import Org.junit.before;*/import Org.junit.test;public class Testmastercrud {/*MasterCurd curd= null; @Before        run public void befor () {   curd=new mastercurd () in front;} */@Test public   void Testfindall () {Mastercurd.findall ();} @Test public   void Insert () {Mastercurd.insert ();} @Test public   void Update () {mastercurd.update ();} @Test public   void Delete () {mastercurd.delete ();}}

} }

Pure Java way to connect to database simple operation

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.