java----jdbc(數據庫的添加,刪除,修改,更新)


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);
		}	
	}
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM