JDBC CURD 的几个DEMO

来源:互联网 发布:微信淘客软件 自动回复 编辑:程序博客网 时间:2024/06/05 16:57

              DEMO1

package com.xiangshuai.jdbc;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;/** * @author xiangshuai * 通用的crud 带占位符操作单条sql * */public class JDBCToos { public static void main(String[] args) { String sql="delete from stu where sid=?"; JDBCToos.update(sql,"S_1013"); } /** * 通用的带占位符增,删,改的操作 * @param sql:要操作数据库的sql * @param args :填充占位符参数的值,如果没有占位直接传个no过来即可 * */ public static void update(String sql,Object... args){ Connection connection=null; PreparedStatement ps=null; try { connection=GeneralConnection.getConnection(); ps=connection.prepareStatement(sql); System.out.println(args.length); if(args[0]!="no"){//如果有参数才执行 for(int i=0;i<args.length;i++){ ps.setObject(i+1, args); } } ps.executeUpdate(); } catch (Exception e) { // TODO: handle exception }finally { if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}

DEMO2

 

package com.xiangshuai.jdbc;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;/** * @author xiangshuai * 通用的crud 带占位符操作单条sql * */public class JDBCToos { public static void main(String[] args) { String sql="delete from stu where sid=?"; JDBCToos.update(sql,"S_1013"); } /** * 通用的带占位符增,删,改的操作 * @param sql:要操作数据库的sql * @param args :填充占位符参数的值,如果没有占位直接传个no过来即可 * */ public static void update(String sql,Object... args){ Connection connection=null; PreparedStatement ps=null; try { connection=GeneralConnection.getConnection(); ps=connection.prepareStatement(sql); System.out.println(args.length); if(args[0]!="no"){//如果有参数才执行 for(int i=0;i<args.length;i++){ ps.setObject(i+1, args); } } ps.executeUpdate(); } catch (Exception e) { // TODO: handle exception }finally { if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}

                    DEMO3

package com.xiangshuai.jdbc;/** * @author xiangshuai * 参数 增,删,改的 sql * 无占位符增,删,改操作通用 * */import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;public class DemoStatement { private Connection connection; private Statement statement; public void cudDB(String sql){ try { connection=GeneralConnection.getConnection(); statement= connection.createStatement(); statement.execute(sql); } catch (Exception e) { e.printStackTrace(); }finally{ if(statement!=null){ try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}

              DEMO4

package com.xiangshuai.jdbc;import java.io.InputStream;/** * @author 香帅 * 单例获取数据库连接的demo * return Connection * */import java.sql.Connection;import java.sql.DriverManager;import java.util.Properties;public class GeneralConnection { private static Connection connection; public static Connection getConnection(){ if(connection==null){ try { //解析jdbc.properties这个配置文件,先将这个配置文件变成流,然后将此流load变成 一个properties map对象,再通过 key获得 value InputStream in = GeneralConnection.class.getClassLoader().getResourceAsStream("pz/jdbc.properties"); //这个url是从src下开始搜索的 Properties properties = new Properties(); properties.load(in); String driver = properties.getProperty("db.driverClassName"); String url = properties.getProperty("db.url"); String username = properties.getProperty("db.username"); String password = properties.getProperty("db.password"); //获得连接二部曲 Class.forName(driver); connection=DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } return connection; } private GeneralConnection() { super(); } public static void main(String[] args) { System.out.println(GeneralConnection.getConnection()); }}

原创粉丝点击