用java代码编写连接数据库操作

package JDBC_Manager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutionException;

import javax.swing.text.DefaultEditorKit.PasteAction;

import org.w3c.dom.html.HTMLHRElement;

import com.sun.corba.se.impl.orbutil.RepositoryIdStrings;
import com.sun.rmi.rmid.ExecPermission;

import Day1.PrepareStatement;

/**
 * 
       在此类中创建并实现所有对数据库表的操作,包括增、删、改、查    
 * 
 *
 */
public class AccountDAO {
     //1、实现通过卡号,查询账户的所有信息。
	public Account queryAccount(String card_id)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rest=null;
		//①、连接数据库
	    Accountutil accountutil=new Accountutil();
	    conn=accountutil.getconnection();
		//②、准备sql语句
		String str="select card_id,card_passward,card_balance,card_name,card_phone from Account where card_id=?";
		//③、发送sql语句
		pstm=conn.prepareStatement(str);
		pstm.setString(1, card_id);		
		rest=pstm.executeQuery();
		Account acc=new Account();
		//④、处理返回的结果集
        while(rest.next()){
		String card_passward=rest.getString("card_passward");
		double card_balance=rest.getDouble("card_balance");
		String card_name=rest.getString("card_name");
		String card_phone=rest.getString("card_phone");
		acc.setCard_balance(card_balance);
		acc.setCard_name(card_name);
		acc.setCard_passward(card_passward);
		acc.setCard_phone(card_phone);
		acc.setCard_id(card_id);
        }
        //关闭资源
	    accountutil.closeconnection(conn, pstm, rest);
		return acc;		
	}
	/**
	 * 2、通过卡号查询某个信息,并修改查询的信息并保存最终的结果。
	 */
	public void UpdateAccount(Account account)throws Exception{
		Connection con=null;
		PreparedStatement past=null;
		//①、连接数据库
		Accountutil accountutil=new Accountutil();
	    con=accountutil.getconnection();
		String str1=account.getCard_passward();
		double dou1=account.getCard_balance();
		String updatesql="update Account set card_passward=? ,card_balance=? ,card_name=? ,card_phone=? where card_id=?";
		past=con.prepareStatement(updatesql);
		past.setString(1, account.getCard_passward());
		past.setDouble(2,account.getCard_balance());
		past.setString(3, account.getCard_name());
		past.setString(4, account.getCard_phone());
		past.setString(5, account.getCard_id());
		past.executeUpdate();		
		accountutil.closeconnection(con, past, null);
	}	
	//3、根据卡号,删除该卡号的所有信息
	public void deleteAccount(String a)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		//①、连接数据库
		Accountutil accountutil=new Accountutil();
	    conn=accountutil.getconnection();
		String deletesql="delete from Account where card_id=?";
		pstm=conn.prepareStatement(deletesql);
		pstm.setString(1, a);
		pstm.executeUpdate();
		accountutil.closeconnection(conn, pstm, null);
		
	}
	//4、增加一行数据,卡号随机生成。
	public void addAccount(Account acc)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rest=null;
		//①、连接数据库
		Accountutil accountutil=new Accountutil();
	    conn=accountutil.getconnection();
		String str2="select seq_newsid.nextval from sys.dual";
		pstm=conn.prepareStatement(str2);
		rest=pstm.executeQuery();
		String str3 = null;
		while(rest.next()){
			str3=rest.getString(1);
		}
	
		String str="insert into Account(card_id,card_passward,card_balance,card_name,card_phone)values(?,?,?,?,?)";
		pstm=conn.prepareStatement(str);
		acc.setCard_id(str3);
		pstm.setString(1, str3);
		pstm.setString(2,acc.getCard_passward() );
        pstm.setDouble(3, acc.getCard_balance());
        pstm.setString(4, acc.getCard_name());
        pstm.setString(5, acc.getCard_phone());
        pstm.executeUpdate();
        accountutil.closeconnection(conn, pstm, rest);        
}
}



//在此类中实现所有工具
package JDBC_Manager;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.*;

import Day1.PrepareStatement;

public class JDBCutil2 {
	private static final Properties prop=new Properties();
	/**
	 * properties 是 map的子类 ,可以通过properties进行读取操作,
	 * 创建.properties文件 用来保存将来容易改变的数据 。例如 url、DriverManager等。
	 */
	static InputStream is=null;	
	static{
		try {
			is=JDBCutil2.class.getResourceAsStream("/JDBC_Manager/JDBC.properties");
			prop.load(is);
			String str1=prop.getProperty("DriverManager");
			Class.forName(str1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
   public Connection getconnection(){
	Connection con=null;
	try {
		String str2=prop.getProperty("url");
		String str3=prop.getProperty("user");
		String str4=prop.getProperty("password");
		con=DriverManager.getConnection(str2,str3,str4);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return con;
	   
   }
   public void closeconnection(Connection con,PreparedStatement pstm,ResultSet rest){
	    
	   try {
		rest.close();
	} catch (SQLException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}	   
	   try {
		rest.close();
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}  	   
	   try {
		con.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
}


package JDBC_Manager;
//在此类中管理表
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.*;

import Day1.PrepareStatement;

public class JDBCutil2 {
	private static final Properties prop=new Properties();
	/**
	 * properties 是 map的子类 ,可以通过properties进行读取操作,
	 * 创建.properties文件 用来保存将来容易改变的数据 。例如 url、DriverManager等。
	 */
	static InputStream is=null;	
	static{
		try {
			is=JDBCutil2.class.getResourceAsStream("/JDBC_Manager/JDBC.properties");
			prop.load(is);
			String str1=prop.getProperty("DriverManager");
			Class.forName(str1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
   public Connection getconnection(){
	Connection con=null;
	try {
		String str2=prop.getProperty("url");
		String str3=prop.getProperty("user");
		String str4=prop.getProperty("password");
		con=DriverManager.getConnection(str2,str3,str4);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return con;
	   
   }
   public void closeconnection(Connection con,PreparedStatement pstm,ResultSet rest){
	    
	   try {
		rest.close();
	} catch (SQLException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}	   
	   try {
		rest.close();
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}  	   
	   try {
		con.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
}

//JDBC工程里的properties文件 取名 JDBC.properties
DriverManager=oracle.jdbc.OracleDriver
user=hr
password=wpp
url=jdbc:oracle:thin:@127.0.0.1:1521:xe



//测试类,可自写
package JDBC_Manager;

import java.util.concurrent.ExecutionException;

public class Text_account {
public static void main(String[] args)throws Exception{
	/*AccountDAO ado=new AccountDAO();
	Account acc=new Account();
	String str="999";
	acc=ado.queryAccount(str);
	System.out.println(acc.getCard_balance()+" "+acc.getCard_name()+acc.getCard_phone());
	*/
	/*
     AccountDAO ado=new AccountDAO();
	Account acc=new Account("999","abc",888.0,"wpp","18839788888");
	ado.UpdateAccount(acc);
	*/
	 
	/*String aString="999"; 
	 AccountDAO ado=new AccountDAO();
	 ado.deleteAccount(aString);
	 */
	Account account=new Account("null","abc",999.0,"wpp","18839788028");
	AccountDAO accDAO=new AccountDAO();
	accDAO.addAccount(account);
}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值