学习_Java_Base_JDBC_Statement_示例

package com.zhaofei.jdbc.study;

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

public class StatementTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StatementTest test = new StatementTest();
		//test.createTable();
		//test.insertTable();
		//test.deleteTable();
		//test.updateTable();
		test.qureyTable();
	}
	public void createTable() {
		DButil util = new DButil();
		Connection conn = util.openConnection();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			String sql = "create table StuTbl(id int, name varchar(20), passwd varchar(20))";
			stmt.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void insertTable() {
		DButil util = new DButil();
		Connection conn = util.openConnection();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			String sql = "insert into stutbl(id, name, passwd)values(1,'tom',20)";
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void deleteTable() {
		DButil util = new DButil();
		Connection conn = util.openConnection();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			String sql = "delete from stutbl where id = 1";
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void updateTable() {
		DButil util = new DButil();
		Connection conn = util.openConnection();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			String sql = "update stutbl set name='bigtom' where id =1";
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void qureyTable() {
		DButil util = new DButil();
		Connection conn = util.openConnection();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			String sql = "select id, name from stutbl";
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				int id  = rs.getInt(1);
				String name = rs.getString("name");
				System.out.println("id=" + id +"  name=" + name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java和MySQL将Base64编码的数据存储到数据库中。首先,你需要创建一个MySQL表来存储数据。表的结构可以包含一个列来存储Base64编码的字符串。 以下是一个示例的MySQL表的创建语句: ```sql CREATE TABLE my_table ( id INT AUTO_INCREMENT PRIMARY KEY, base64_data TEXT ); ``` 接下来,在Java中,你可以使用JDBC连接MySQL数据库并执行插入操作。你需要将Base64编码的数据转换为字节数组,然后将字节数组插入到数据库中。 以下是一个示例Java代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Base64; public class Main { public static void main(String[] args) { String base64Data = "SGVsbG8gV29ybGQh"; // Base64编码的数据 // 连接到MySQL数据库 try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_database", "username", "password")) { // 插入数据 String sql = "INSERT INTO my_table (base64_data) VALUES (?)"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { byte[] data = Base64.getDecoder().decode(base64Data); // 将Base64编码的字符串解码为字节数组 stmt.setBytes(1, data); // 将字节数组设置为参数值 stmt.executeUpdate(); // 执行插入操作 } } catch (SQLException e) { e.printStackTrace(); } } } ``` 以上代码中,你需要将`jdbc:mysql://localhost:3306/my_database`替换为你的MySQL数据库连接字符串,`username`和`password`替换为你的数据库用户名和密码。 这样,你就可以将Base64编码的数据存储到MySQL数据库中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值