JDBC学习

本文详细介绍JDBC的基础操作,包括查询、更新、插入和删除等基本功能,并演示如何处理大文本数据的读取与写入。

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

JDBC学习

体验JDBC:

案例一

package cn.taxue.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class JdbcDemo01 {
	public static void main(String[] args) throws SQLException {
		DriverManager.registerDriver(new Driver());// 注册驱动
		//获取连接
		Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/gmcms", "root", "root");
		Statement statement = connection.createStatement();//获取操作的sql语句
		ResultSet resultset = statement.executeQuery("select * from T_USER");	//操作sql语句获得结果集合

		 while (resultset.next()) {
			System.out.print("id:"+resultset.getInt("id")+" ");
			System.out.print("role:"+resultset.getInt("role")+"\t");
			System.out.print("udesc:"+resultset.getString("udesc")+"\t");
			System.out.print("时间类型:"+resultset.getDate("createtime"));
			System.out.print("username:"+resultset.getString("username"));
			System.out.println( "\t");
		}
	}
}
结果:

id:15 role:1	udesc:超级用户-勿删	时间类型:2013-08-02username:admin	
id:16 role:2	udesc:编辑人员王铮	时间类型:2013-08-13username:wangzheng 	
id:17 role:2	udesc:编辑人员于然	时间类型:2013-07-22username:yuran	
id:18 role:1	udesc:开发	时间类型:2013-07-22username:zff123	

2.mysql驱动加载问题分析

package cn.taxue.jdbcdemo;

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

public class JdbcDemo02 {
	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {
		// DriverManager.registerDriver(new Driver());// 注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		// 获取连接
		Connection connection = DriverManager.getConnection(
				"jdbc:mysql://127.0.0.1:3306/gmcms", "root", "root");
		Statement statement = connection.createStatement();// 获取操作的sql语句
		ResultSet resultset = statement.executeQuery("select * from T_USER"); // 操作sql语句获得结果集合
		while (resultset.next()) {
			System.out.println(resultset.getString(2));
		}
	}
}
结果:

admin
wangzheng 
yuran

3,更新表操作:

package cn.taxue.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo03 {
	public static void main(String[] args)  {
		String username="root";
		String password="root";
		String url ="jdbc:mysql://127.0.0.1:3306/oa";
		String sql="update user set name= '王' where id = 1";
		Connection connection=null;
		Statement resultset=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			//获得连接
			connection=DriverManager.getConnection(url, username, password);
			 resultset = connection.createStatement();//获取sql语句的对象
			 int updatedate = resultset.executeUpdate(sql);
			 System.out.println("若为1表示更新成功0表示失败:"+updatedate);
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动失败");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("连接失败");
			e.printStackTrace();
		}
	}
}

4.工具类的抽取以及配置文件的是使用

mysql.properties

username=root
password=root
url=jdbc\:mysql\://127.0.0.1\:3306/oa

JdbcUtils

package cn.taxue.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class JdbcUtils {
	private static final String username;
	private static final String password;
	private static final String url;

	static {
		// 配置文件
		username = ResourceBundle.getBundle("mysql").getString("username");
		password = ResourceBundle.getBundle("mysql").getString("password");
		url = ResourceBundle.getBundle("mysql").getString("url");

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动");
			e.printStackTrace();
		}
	}

	/**
	 * 得到一个Connection 对象的方法
	 * 
	 * @return connection
	 * @throws SQLException
	 * @author xue
	 */
	public static Connection getConnection() throws SQLException {
		Connection connection = DriverManager.getConnection(url);
		return connection;

	}

	/**
	 * 关闭连接
	 * 
	 * @param connection
	 * @throws SQLException
	 * @author xue
	 */
	public static void closeConnection(Connection connection)
			throws SQLException {
		if (connection != null) {
			connection.close();
		}
	}

	public static void closeStatement(Statement statement) throws SQLException {
		if (statement != null) {
			statement.close();
		}
	}

	public static void closeStatement(ResultSet statement) throws SQLException {
		if (statement != null) {
			statement.close();
		}
	}

}

5.增删改查操作

package cn.taxue.jdbcdemo;

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

import org.junit.Test;

public class JdbcSelect {
	@Test
	public void select() {
		String sql = "select * from user";
		// 得到Connection对象
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JdbcUtils.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);

			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				System.out.println("id:" + id + "  name:" + name);

			}
		} catch (SQLException e) {
			System.out.println("得到一个Connection 对象的方法失败");
			e.printStackTrace();
		} finally {

			try {
				JdbcUtils.closeConnection(connection);
				JdbcUtils.closeStatement(statement);
				JdbcUtils.closeConnection(connection);
			} catch (SQLException e) {
				System.out.println("关闭失败");
				e.printStackTrace();
			}

		}

	}

	@Test
	public void update() {

		String sql = "update user set name ='王1' where id = 1";
		// 得到Connection对象
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JdbcUtils.getConnection();
			statement = connection.createStatement();
			int aa = statement.executeUpdate(sql);
			System.out.println(aa);

		} catch (SQLException e) {
			System.out.println("得到一个Connection 对象的方法失败");
			e.printStackTrace();
		} finally {

			try {
				JdbcUtils.closeConnection(connection);
				JdbcUtils.closeStatement(statement);
				JdbcUtils.closeConnection(connection);
			} catch (SQLException e) {
				System.out.println("关闭失败");
				e.printStackTrace();
			}

		}

	}

	@Test
	public void insert() {

		String sql = "insert into user values (null,'小名',112)";
		// 得到Connection对象
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JdbcUtils.getConnection();
			statement = connection.createStatement();
			int aa = statement.executeUpdate(sql);
			System.out.println(aa);

		} catch (SQLException e) {
			System.out.println("得到一个Connection 对象的方法失败");
			e.printStackTrace();
		} finally {

			try {
				JdbcUtils.closeConnection(connection);
				JdbcUtils.closeStatement(statement);
				JdbcUtils.closeConnection(connection);
			} catch (SQLException e) {
				System.out.println("关闭失败");
				e.printStackTrace();
			}

		}

	}

	@Test
	public void delete() {

		String sql = "delete from user where name = '小名'";
		// 得到Connection对象
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JdbcUtils.getConnection();
			statement = connection.createStatement();
			int aa = statement.executeUpdate(sql);
			System.out.println(aa);

		} catch (SQLException e) {
			System.out.println("得到一个Connection 对象的方法失败");
			e.printStackTrace();
		} finally {

			try {
				JdbcUtils.closeConnection(connection);
				JdbcUtils.closeStatement(statement);
				JdbcUtils.closeConnection(connection);
			} catch (SQLException e) {
				System.out.println("关闭失败");
				e.printStackTrace();
			}

		}

	}

}

6.大文本写入和获取的处理

package cn.taxue.jdbcdemo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

public class JdbcText {
	@Test
	public void addText() throws SQLException, FileNotFoundException {
		String sql = "insert into mytext values(null,?)";
		Connection connection = JdbcUtils.getConnection();
		PreparedStatement pst = connection.prepareStatement(sql);

		File file = new File("a.txt");
		FileReader fileReader = new FileReader(file);

		pst.setCharacterStream(1, fileReader, (int) file.length());
		pst.executeUpdate();
		pst.close();
		connection.close();
	}

	@Test
	public void getText() throws SQLException, IOException {
		String sql = "select * from mytext where id = ?";
		Connection connection = JdbcUtils.getConnection();
		PreparedStatement pst = connection.prepareStatement(sql);

		pst.setInt(1, 1);

		ResultSet resultSet = pst.executeQuery();
		if (resultSet.next()) {
			Reader reader = resultSet.getCharacterStream("msg");

			BufferedReader bufferedReader = new BufferedReader(reader);
			BufferedWriter bw = new BufferedWriter(new FileWriter("d:/a.txt"));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				bw.write(line);
				bw.newLine();
				bw.flush();
			}
			bw.close();
			bufferedReader.close();
		}

		resultSet.close();
		pst.close();
		connection.close();

	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值