首页 > 代码库 > 纯java方式连接数据库简单操作
纯java方式连接数据库简单操作
//JDBC全称java datebese connection --java数据库连接
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; /*数据库帮助类*/ 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"); driverClass=rb.getString("driverClass"); url=rb.getString("url"); user=rb.getString("user"); password=rb.getString("password"); try { //加载驱动 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 (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (null!=rs) { try { stme.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (null!=rs) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.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"; //加载驱动 try { //Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载成功"); 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); } } //插入 public static void Insert (){ Connection conn=null; Statement stme=null; String sql="INSERT INTO master (name, password, money) VALUES (‘张政‘,‘123‘, 100)"; try { //获得连接对象 conn= DBUtils.getConn(); System.out.println("连接成功"); //获取Statement对象 stme=conn.createStatement(); //执行语句 stme.execute(sql); } catch (Exception e) { e.printStackTrace(); }finally{ DBUtils.closeAll(null, stme, conn); } } //更新 public static void Update(){ Connection conn=null; Statement stme=null; String sql="UPDATE master SET name=‘张军‘,money=50 WHERE id=6"; try { //获得连接对象 conn= DBUtils.getConn(); System.out.println("连接成功"); //获取Statement对象 stme=conn.createStatement(); //执行语句 stme.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); }finally{ DBUtils.closeAll(null, stme, conn); } } //删除 public static void Delete(){ Connection conn=null; Statement stme=null; String sql="DELETE FROM master WHERE id=7"; try { //获得连接对象 conn= DBUtils.getConn(); System.out.println("连接成功"); //获取Statement对象 stme=conn.createStatement(); //执行语句 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 在前面运行 public void befor(){ curd=new MasterCurd(); }*/ @Test public void testFindAll(){ MasterCurd.findAll(); } @Test public void Insert(){ MasterCurd.Insert(); } @Test public void Update(){ MasterCurd.Update(); } @Test public void Delete(){ MasterCurd.Delete(); } }
} }
纯java方式连接数据库简单操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。