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