一:JDBC


 sun:提供了一套通用性的接口:可以连接任何的数据库: 
 连接数据库的具体得到实例,具体的数据库厂商实现的。 
 
 连接数据的步骤(别忘了复制jar包):( 
 1)注册驱动: Class.forName():DriverManager 
 (2)获得链接对象:Connection 
 (3)创建sql容器:语句: 
 (4)执行sql语句:: stmt
 (5)查询操作:遍历结果集:ResultSet 
 (6)关闭资源: 
 
 
 ResultSet:详解(更多可以看API
 封装了结果集的对象:内部有一个可移动的光标,默认情况,指向第一条记录集的上一条记录:  
 
 next();光标下移动一次:返回的boolean的值;判断是否有结果可以被遍历:  
 previous();光标上移动一次: 
 last()移动到最后一行: 
 afterLast();移动到最后一行之后:
 beforeFirst()移动到第一行的之前:
 first() 
 
 getObject(); 
 getInt(); 
 的GetString(); 
 getFloat(); 
 getDouble()
 getDate(); 
 getXxx()
 
 的的getMetaData()获得结果集的元数据:(重要)

package com.yidongxueyuan.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class StudentDaoImpl {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc01", "root", "root");
			
			Statement stmt = conn.createStatement();
			
			String sql ="select * from student ";
			ResultSet rs = stmt.executeQuery(sql);
			
			/*while(rs.next()){
				int id = rs.getInt("sid");
				String name = rs.getString("sname");
				System.out.println(id+"  "+name);
			}
			*/
			rs.next(); 
			//上移动一次: 
//			rs.previous();
			
//			rs.last();
//			rs.first() ;
			rs.beforeFirst();

			while(rs.next()){
//				Object object = rs.getObject("1");
				int id = rs.getInt("sid");
				String name = rs.getString("sname");
				System.out.println(id+"  "+name);
			}
			
			
			rs.close(); 
			stmt.close(); 
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			
			
		}
		
		
	}
}


 // mysql封装:工具类: 

封装好的UTIL:

package com.yidongxueyuan.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/*
 * 对JDBC 进行封装: 
 */
public class JdbcUtils2 {
	
	//准备数据库的四大参数: 
	private static final String driver ="com.mysql.jdbc.Driver"; 
	private static final String url ="jdbc:mysql://localhost:3306/jdbc01"; 
	private static final String username ="root"; 
	private static final String password ="root";
	
	
	/*
	 * 驱动: 只需要注册一次就OK 
	 * 
	 */
	
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 1:创建工具类: 直接获得一个连接对象: 
	 */
	public static Connection getConnection (){
		try {
			Connection connection = DriverManager.getConnection(url, username, password);
			return  connection; 
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/*
	 * 定义一个方法: 释放资源: 直接将rs stmt conn 全部释放: 
	 */
	public static void release(ResultSet rs ,Statement stmt , Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn=null;
		}
		
	}
	
}

调用工具类实现操作:

package com.yidongxueyuan.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * 使用工具类: 对代码进行重构: 
 */
public class StudentDaoImpl2 {
	public static void main(String[] args) {
		Connection conn=null; 
		Statement stmt=null;
		ResultSet rs=null;
		try {
			//使用工具类 获得一个连接对象: 
			 conn = JdbcUtils2.getConnection();
			
			 stmt = conn.createStatement();
			
			String sql ="select * from student ";
			 rs = stmt.executeQuery(sql);
			
		

			while(rs.next()){
//				Object object = rs.getObject("1");
				int id = rs.getInt("sid");
				String name = rs.getString("sname");
				System.out.println(id+"  "+name);
			}
			
			
		
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			JdbcUtils2.release(rs, stmt, conn);
		}
		
		
	}
}

上面的代码是硬编码,如果是软编码的话步骤如下:

1个方言文件内容:

# this is my mysql configuration  
driver =com.mysql.jdbc.Driver
url =jdbc:mysql://localhost:3306/jdbc01
username =root
password =root

2 jdbcutil.java读取如下:

package com.yidongxueyuan.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
 * 读取外部的配置文件
 */
public class JdbcUtil {
	private static final Properties p= new Properties();
	private static String url; 
	private static  String driver; 
	private static String username; 
	private static String password; 
	
	
	static{
		try {
			//将外部的配置文件进行读取: 类的加载器: 
			InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties");
			p.load(in);
			
		    driver = p.getProperty("driver");
			Class.forName(driver);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 1:创建工具类: 直接获得一个连接对象: 
	 */
	public static Connection getConnection (){
		try {
			url = p.getProperty("url");
			username = p.getProperty("username");
			password = p.getProperty("password");// 
			Connection connection = DriverManager.getConnection(url, username, password);
			return  connection; 
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	//释放连接: 
	/*
	 * 定义一个方法: 释放资源: 直接将rs stmt conn 全部释放: 
	 */
	public static void release(ResultSet rs ,Statement stmt , Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn=null;
		}
		
	}
	
	/*public static void main(String[] args) {
		Connection connection = getConnection();
		System.out.println(connection);
	}*/
	
	
}


 
 使用上面的工具类完成增加改查操作:

 

package com.yidongxueyuan.dao2;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;

import org.junit.Test;

import com.yidongxueyuan.dao.JdbcUtil;

public class StudentCRUD {
	
	//insert 插入: 
	@Test
	public void insertStudent() throws Exception {
		
		Connection conn = JdbcUtil.getConnection();
		Statement stmt = conn.createStatement();
		int num= stmt.executeUpdate("insert into student (sname, birthday) " +
				"values('xxx','"+new java.sql.Date(System.currentTimeMillis())+"')");
		
		if(num>0){
			System.out.println("插入成功");
		}else{
			
			System.out.println("插入失败");
		}
		
		JdbcUtil.release(null, stmt, conn);
		
	}
	//删除: 
	@Test
	public void deleteStudent() throws Exception {
		
		Connection conn = JdbcUtil.getConnection();
		Statement stmt = conn.createStatement();
		String sql="delete from student where sid=2 ";
		int num= stmt.executeUpdate(sql);
		System.out.println("num:"+num);
		if(num>0){
			System.out.println("删除成功");
		}else{
			
			System.out.println("删除失败");
		}
		JdbcUtil.release(null, stmt, conn);
	}
	
	//修改: 
	@Test
	public void updateStudent() throws Exception {
		
		Connection conn = JdbcUtil.getConnection();
		Statement stmt = conn.createStatement();
		String sql="update student set sname='yyyyy' where sid =3 ";
		int num= stmt.executeUpdate(sql);
		System.out.println("num:"+num);
		if(num>0){
			System.out.println("修改成功");
		}else{
			
			System.out.println("修改失败");
		}
		JdbcUtil.release(null, stmt, conn);
	}
	
	//查: 
	
}



 
 
  

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐