jdbc连接数据库源码

  1. JDBC原理
    Java高级静态语言 它帮助我们做了什么?

它提供了一套接口,用于接入不同的数据库!基本上主流的(RDB)数据库都支持!

这套接口的名字叫做JDBC。(Java DataBase Connectivity)!

Java应用程序通过JDBC来连接数据库时,其实就是在Java平台准备一套接口(接口:interface 它要使用必须要找寻对应的实现类!),然后不同的数据库厂商提供这些接口的实现类(驱动),然后再通过Java提供的驱动管理程序对驱动进行验证及安装,再然后我们就可以正常使用了。
public class TestJDBC {

public static void main(String[] args) {
	Connection conn = null;
	Statement statement = null;
	ResultSet rs = null;
	try {
		// 1.使用DriverManager管理驱动
		// registerDriver 注册/加载驱动
		// Driver是一个接口  它需要寻找对应的实现类(数据库厂商提供的驱动类们)
		Driver driver = new com.mysql.jdbc.Driver();
		DriverManager.registerDriver(driver);
		
		// 2.建立和数据库的连接 Connection
		// 类似于http://192.168.12.138:8080/各级资源目录
		// 数据库地址:协议://主机地址/数据库名
		String url = "jdbc:mysql://127.0.0.1:3306/myschool";
		// 数据库用户名
		String user = "root";
		// 数据库用户密码
		String password = "root";
		conn = DriverManager.getConnection(url, user, password);
		
		// 3.使用Statement执行SQL语句
		statement = conn.createStatement();
		String sql = "select * from student";
		rs = statement.executeQuery(sql);
		
		// 4.对返回的数据结果集ResultSet进行解析
		// 这个结果集可以将其看作是一个二维表 
		// boolean next() 移动指针 判断是否有下一行数据
		while(rs.next()) {
			// int getInt(int columnIndex) 根据列索引取列的数据
			// int getInt(String columnLabel) 根据列名取列的数据
			int id = rs.getInt(1);
			String stuName = rs.getString("stuName");
			String address = rs.getString("address");
			System.out.println(id+" -- "+stuName+" -- "+address);
		}
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		// 5.关流
		// 先开的后关
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

}

DriverManager驱动管理器

// 在MySQL提供的驱动Driver类中,它自己有一个静态代码块,当Driver类被加载的时候,它会自动执行注册驱动代码
// 所以意味着我们只需要保证这个类被加载 就可以确保驱动会被正常管理起来了!Class.forName(“com.mysql.jdbc.Driver”);

//
// Register ourselves with the DriverManager
//
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException(“Can’t register driver!”);
}
}
3. JDBC的增删改查
添加
public class JDBCDemo2 {

public static void main(String[] args) {
Connection conn = null;
Statement st = null;
try {
// 1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
// 2.定义sql
String sql = “insert into account values(null,‘王五’,3000)”;

	// 3.获取Connection数据库连接对象
	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/flq", "root", "root");
	
	// 4.获取执行sql的对象 Statement
	st = conn.createStatement();
	// 5.执行sql  影响的行数
	int count = st.executeUpdate(sql);
	// 6.处理结果
	System.out.println(count);
	if(count > 0){
		System.out.println("添加成功!");
	}else{
		System.out.println("添加失败!");
	}
	
} catch (ClassNotFoundException e) {
	
	e.printStackTrace();
}catch (SQLException e) {
	
	e.printStackTrace();
}finally{
	// 7.释放资源
	// 避免空指针异常
	if(st != null){	
		try {
			st.close();
		} catch (SQLException e) {			
			e.printStackTrace();
		}
	}
	if(conn != null){	
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

}
修改
public class JDBCDemo3 {

public static void main(String[] args) {
	Connection conn = null;
	Statement st = null;
	
	try {
		// 1.注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		// 2.定义SQL
		String sql = "update account set balance = 1500 where name='王五'";
		
		// 3.获取数据库连接对象
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/flq","root","root");
	    // 4.获取执行sql的对象 Statement
		st = conn.createStatement();
		// 5.执行SQL
		int count = st.executeUpdate(sql);
		// 6.处理结果
		System.out.println(count);
		if(count > 0){
			System.out.println("修改成功!");
		}else{
			System.out.println("修改失败!");
		}
	
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		// 7.释放资源
		// 防止空指针
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
`查询
public class JDBCDemo7 {

public static void main(String[] args) {
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;
	try {
		// 1.注册驱动
		Class.forName("com.mysql.jdbc.Driver");

		// 2创建数据库连接对象
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/flq", "root", "root");

		// 3.定义SQL语句
		String sql = "select * from account";

		// 4.获取执行SQL的对象
		st = conn.createStatement();
		// 5.执行SQL
		rs = st.executeQuery(sql);
		// 6.处理结果
		// 6.1让游标向下移动一行
		while(rs.next()){
		// 循环判断游标是否是最后一行末尾
		// 6.2 获取数据
		int id = rs.getInt(1);
		String name = rs.getString("name");
		double balance = rs.getDouble(3);
		System.out.println(id+"---"+name+"---"+balance);
		}
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		// 7.释放资源
		// 防止空指针
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

}

PreparedStatement接口的使用-特别的执行SQL语句的接口

public class TestLogin2 {

public static void main(String[] args) {

	Scanner input = new Scanner(System.in);
	System.out.print("请输入您的用户名:");
	String username = input.next();
	System.out.print("请输入您的密码:");
	String password = input.next();

	// 查询数据库
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	try {
		// 1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		// 2.建立连接
		conn = DriverManager.getConnection("jdbc:mysql:///kgcnews", "root", "root");
		// 3.编写SQL语句  将参数设立为占位符
		// statement = conn.createStatement(); 
		String sql = "select * from news_user where userName = ? and password = ?";
		// 4.创建语句执行者
		ps = conn.prepareStatement(sql);
		// 5.给占位符赋值
		ps.setString(1, username);
		ps.setString(2, password);
		// 6.执行SQL语句
		rs = ps.executeQuery();
		// 6.解析结果集
		if (rs.next()) {
			System.out.println("登录成功!");
		} else {
			System.out.println("登录失败!");
		}

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		// 先开的后关
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

作者:weixin_44793533
来源:CSDN
原文:https://blog.csdn.net/weixin_44793533/article/details/91038148
版权声明:本文为博主原创文章,转载请附上博文链接!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值