java连接数据库Java DataBase Connectivity(MySQL)

java连接数据库Java DataBase Connectivity

大概分为四个步骤

第一步:导入驱动,加载具体的驱动类
第二步:与数据库建立连接
第三步:发送SQl,执行(如果是查询,就会出现返回的结果,其他的没有)
*第四步:接受返回的结果


会出现两个异常(SQLException ClassNotFoundException )

一.导入驱动(需要下载)

二.加载具体的驱动类Class.forName("com.mysql.jdbc.Driver");

 

三.与数据库建立连接
private static final String url="jdbc:mysql://47.114.87.54:3306/pet";//连接字符串
private static final String username="pet_reader";//用户名
private static final String password="pet@123"; //密码
connection = (Connection) DriverManager.getConnection(url,username,password);

这里连接的是云服务器,如果要连接到自己的电脑上
private static final String url="jdbc:mysql://localhost:3306/数据名";//连接字符串

 

四.发送SQl,执行
stmt = (Statement) connection.createStatement();
String sql="insert into goods values(1,'2',1)";

如果是,删除,修改的话,就会返回有多少条数,删除,修改
int count = stmt.executeUpdate(sql);//返回值表示增删改几条数据


*五.如果是查询,返回的结果集用ResultSet来接收,用循环来输出结果
ResultSet rs=null;
rs= (ResultSet) stmt.executeQuery(sql);    
while(rs.next())
{
    //rs是一行的结果,如果字段类型为整形,就用getInt("字段名"),来获取,并且返回的结果也为整形

    //如果字段的字符串类型,就用getString("字段名");并且返回的结果也为字符串
}

import java.awt.List;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import sun.*;
import java.util.*;
public class JDBCDemo {
	private static final String url="jdbc:mysql://47.114.87.54:3306/pet";//连接字符串
	private static final String username="pet_reader";//用户名
	private static final String password="pet@123"; //密码
	//增删改
    public static void  update() throws ClassNotFoundException {
    	Connection connection=null;
    	Statement stmt=null;
    	try {
    		//a.导入驱动,加载具体的驱动类
        	Class.forName("com.mysql.jdbc.Driver");
        	//b.与数据库建立连接
        	connection = (Connection) DriverManager.getConnection(url,username,password);
        	//c.发送SQl,执行
        	stmt = (Statement) connection.createStatement();
        	String sql="insert into goods values(1,'2',1)";
        	int count = stmt.executeUpdate(sql);//返回值表示增删改几条数据
        	if(count>0)
        	{
        		System.out.println("增删改成功");
        	}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	catch (SQLException e) {
			e.printStackTrace();
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
    	finally {
    		try {
    			stmt.close();
            	connection.close();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
    		
    	}
    }
    //查询
    public static ArrayList<Pet>  query() throws ClassNotFoundException {
    	Connection connection=null;
    	Statement stmt=null;
    	ResultSet rs=null;
    	try {
    		//a.导入驱动,加载具体的驱动类
        	Class.forName("com.mysql.jdbc.Driver");
        	//b.与数据库建立连接
        	connection = (Connection) DriverManager.getConnection(url,username,password);
        	//c.发送SQl,执行
        	stmt = (Statement) connection.createStatement();
        	String sql="select * from pet";
        	
        	rs= (ResultSet) stmt.executeQuery(sql);//返回值表示增删改几条数据
        	
        	
        	while(rs.next())
        	{
        		
        	}
        	
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	catch (SQLException e) {
			e.printStackTrace();
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
    	finally {
    		try {
    			rs.close();
    			stmt.close();
            	connection.close();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
    		
    	}
    	return list;
    }
    public static void main(String[] args) throws ClassNotFoundException
    {
//    	update();
    	query();
    	System.out.print("成功");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值