java (功能篇) java数据库连接

本文介绍了一个使用Java进行数据库操作的示例项目,包括连接数据库、查询、插入、删除及更新数据等基本功能。示例中详细展示了如何通过Java代码实现与MySQL数据库的交互,并提供了错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.emc.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.emc.model.DyLun;
import com.emc.service.DyLunService;



public class DataBase {
	private static final String DBDRIVER="com.mysql.jdbc.Driver";//驱动类类名

	private static final String DBNAME="iiiis";//数据库名

	private static final String DBURL="jdbc:mysql://192.168.0.32:3306/"+DBNAME;//连接URL

	private static final String DBUSER="root";//数据库用户名

	private static final String DBPASSWORD="Vixuan724";//数据库密码

	private static Connection conn=null;

	private static PreparedStatement ps=null;

	private static ResultSet rs=null;
	
	//获取数据库连接

	public static Connection getConnection(){

	try{

	Class.forName(DBDRIVER);//注册驱动

	conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//获得连接对象
	
	System.out.println("数据库连接成功!");

	}catch(ClassNotFoundException e){//捕获驱动类无法找到异常

	e.printStackTrace();

	}catch(SQLException e){//捕获SQL异常

	e.printStackTrace();

	}

	return conn;

	}
	
	
	//查询数据

	public static ResultSet select(String sql) throws Exception{

	try{
	if(conn == null || conn.isClosed() == true)
	conn=getConnection();

	ps=conn.prepareStatement(sql);

	rs=ps.executeQuery(sql);

	return rs;

	}catch(SQLException sqle){

	throw new SQLException("select data Exception: "+sqle.getMessage());

	}catch(Exception e){

	throw new Exception("System error: "+e.getMessage());

	}

	}
	
	//插入数据

	public static int insert(String sql)throws Exception{

	int num=0;//计数

	try{
    if(conn == null || conn.isClosed() == true)
	conn=getConnection();

	ps=conn.prepareStatement(sql);

	num=ps.executeUpdate();

	}catch(SQLException sqle){

	throw new SQLException("insert data Exception: "+sqle.getMessage());

	}finally{

	try{

	if(ps!=null){

	ps.close();

	}

	}catch(Exception e){

	throw new Exception("ps close exception: "+e.getMessage());

	}

	try{

	if(conn!=null){

	conn.close();

	}

	}catch(Exception e){

	throw new Exception("conn close exception: "+e.getMessage());

	}

	}

	return num;

	}
	
	
	
	//删除数据

	public int delete(String sql)throws Exception{

	int num=0;//计数

	try{
    if(conn == null || conn.isClosed() == true)
	conn=getConnection();

	ps=conn.prepareStatement(sql);

	num=ps.executeUpdate();

	}catch(SQLException sqle){

	throw new SQLException("delete data Exception: "+sqle.getMessage());

	}finally{

	try{

	if(ps!=null){

	ps.close();

	}

	}catch(Exception e){

	throw new Exception("ps close Exception: "+e.getMessage());

	}

	try{

	if(conn!=null){

	conn.close();

	}

	}catch(Exception e){

	throw new Exception("conn close Exception: "+e.getMessage());

	}

	}

	return num;

	}
	
	//修改数据

	public static int update(String sql)throws Exception{

	int num=0;//计数

	try{
	if(conn == null || conn.isClosed() == true)
	conn=getConnection();

	ps=conn.prepareStatement(sql);

	num=ps.executeUpdate();

	}catch(SQLException sqle){

	throw new SQLException("update data Exception: "+sqle.getMessage());

	}finally{

	try{

	if(ps!=null){

	ps.close();

	}

	}catch(Exception e){

	throw new Exception("ps close Exception: "+e.getMessage());

	}

	try{

	if(conn!=null){

	conn.close();

	}

	}catch(Exception e){

	throw new Exception("conn close Excepiton: "+e.getMessage());

	}

	}

	return num;

	}
	
	
	public static void main(String[] args){
		
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值