zoukankan      html  css  js  c++  java
  • javajdbc(数据库的添加,删除,修改,更新)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Mtest4Demo {
    /*
     * jdbc工具类
     * 提供获取连接和释放资源的方法
     */
    	public static Connection getConnection() throws ClassNotFoundException {
    		Connection conn=null;
    		try {
    		  Class.forName("com.mysql.jdbc.Driver");
    		  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/study","root","root");
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		
    		return conn;
    	}
    	public static void release(Connection conn,PreparedStatement pstmt,ResultSet rSet) {
    		if(rSet!=null)
    		{
    			try {
    				rSet.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		if(conn!=null)
    		{
    			try {
    				conn.close();
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    		if(pstmt!=null)
    		{
    			try {
    				pstmt.close();
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	 }
    	//添加信息
    	public int testAdd(String sqlString) {
    		Connection conn=null;
    		PreparedStatement pstmt=null;
    		Mtest4Demo mtest4Demo=new Mtest4Demo();
    		try {
    			conn=mtest4Demo.getConnection();
    			pstmt=conn.prepareStatement(sqlString);
    			int row=pstmt.executeUpdate();
    			if(row>0)
    			{
    				return 1;
    			}else {
    				return 0;
    			}
    			
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return 0;
    	}
    	public int testDeleteByName(String username) {
    		Connection conn=null;
    		PreparedStatement pstmt=null;
    		Mtest4Demo mtest4Demo=new Mtest4Demo();
    		try {
    			conn=mtest4Demo.getConnection();
    			String sqlString="delete from login where "+"username='"+username+"'";
    			pstmt=conn.prepareStatement(sqlString);
    			int row=pstmt.executeUpdate();
    			if(row>0)
    			{
    				return 1;
    			}else {
    				return 0;
    			}
    			
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return 0;
    	}
    	//根据姓名修改相关的信息
    	public int UpdateByName(String name,String username,String password) {
    		Connection conn=null;
    		PreparedStatement pstmt=null;
    		Mtest4Demo mtest4Demo=new Mtest4Demo();
    		try {
    			conn=mtest4Demo.getConnection();
    			String sqlString="update login set username=?,password=? where username=?";
    			pstmt=conn.prepareStatement(sqlString);
    			pstmt.setString(1, username);
    			pstmt.setString(2, password);
    			pstmt.setString(3, name);
    			int row=pstmt.executeUpdate();
    			if(row>0)
    			{
    				return 1;
    			}else {
    				return 0;
    			}
    			
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return 0;
    	}
    }
    

      

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class Mtest5Demo {
    	public static void main(String[] args) throws ClassNotFoundException, SQLException {
    		Mtest4Demo mtest4Demo=new Mtest4Demo();
    		Connection conn=null;
    		PreparedStatement pstmt=null;
    		ResultSet rSet=null;
    		try {
    			conn=mtest4Demo.getConnection();
    			String sqlString="select *from login";
    			pstmt=conn.prepareStatement(sqlString);
    			rSet=pstmt.executeQuery();
    			while(rSet.next())
    			{
    				System.out.println(rSet.getString("username")+"---"+rSet.getString("password"));
    			}
    			String sqlString2="insert into login values ('test','test')";
    			int row=mtest4Demo.testAdd(sqlString2);
    			if(row==1)
    				System.out.println("信息添加成功");
    			else {
    				System.out.println("信息添加失败");
    			}
    			String nameString="zyz";
    			int delete=mtest4Demo.testDeleteByName(nameString);
    			if(delete==1) {
    				System.out.println("删除成功");
    			}else {
    				System.out.println("删除失败");
    			}
    			String name="test";
    			String username="1234567890";
    			String password="1234567890";
    			int update=mtest4Demo.UpdateByName(name, username, password);
    			if(update==1) {
    				System.out.println("信息修改成功");
    			}else {
    				System.out.println("数据修改失败");
    			}
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}finally {
    			mtest4Demo.release(conn, pstmt, rSet);
    		}	
    	}
    }
    

      

    一纸高中万里风,寒窗读破华堂空。 莫道长安花看尽,由来枝叶几相同?
  • 相关阅读:
    【极角排序、扫描线】UVa 1606
    【计算几何】是时候知道这些函数了
    【技巧性(+递归运用)】UVa 1596
    【策略】UVa 11389
    【策略】UVa 1344
    夏季吃西瓜,会勤上厕所,会导致体内缺水,导致便秘,,会长豆豆
    Alt+Shift+R组合键,用来在一个java文件中批量的重命名变量。
    Pycharm-professional-2017.2.3破解安装
    通过反编译深入理解Java String及intern
    转:MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
  • 原文地址:https://www.cnblogs.com/byczyz/p/11161510.html
Copyright © 2011-2022 走看看