插入xml文件到mysql

本文详细介绍了如何使用Java连接MySQL数据库进行数据存储操作,包括数据的插入和查询。

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

package cn.jn.bq.mawt.test;

import java.io.*;
import java.sql.*;

/*
 * DB:        bq_test
 * Table:    tsblob
 * create table tsblob(
 *     tid bigint not null auto_increment primary key,
 *     tname varchar(20) not null,
 *     tcontent text
 * );
 */

public class DBBlob {

    private String driver="com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/bq_test";
    private String user = "root";
    private String pass = "123456";
    
    public DBBlob(){
        
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DBBlob db = new DBBlob();
        //db.insertCBlob();
        db.queryCBlob();
    }
    //Statement stmt = conn.createStatement();
    //ResultSet rs = stmt.executeQuery("select tname,ulevel,tfile from template");
    
    public Connection getConn()
    {
        try {            
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,pass);
            return conn;
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     *
     */
    private void insertCBlob() {
        BufferedInputStream in = null;
        BufferedReader br = null;
        Connection con = getConn();
        
        File text1 = new File("/home/grid/taobao/files/template.xml");
        
        try {
            con.setAutoCommit(false);
            //in = new BufferedInputStream(new FileInputStream(image));
            // 用下面的方法可以获得文件中的中文
            // br=new BufferedReader(new InputStreamReader(new
            // FileInputStream(text1)));
            // br=new BufferedReader(new InputStreamReader(new
            // FileInputStream(text1),"UTF-8"));

            br = new BufferedReader(new FileReader(text1));
            
            Statement stmt = con.createStatement();
            String insql = "insert into tsblob(tname,tcontent) values(?,?)";
            PreparedStatement ps=con.prepareStatement(insql);
            ps.setString(1, "marvin");
            ps.setCharacterStream(2, br, (int)text1.length());
            
            
//            br=new BufferedInputStream(new FileInputStream(text1));
//            ps = con.prepareStatement("insert into photostu(id,name,photo,descr) values(null,?,?,?)");
//            ps.setString(1, "liu1");
//            ps.setBinaryStream(2, in, (int) image.length());
//            System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
//            ps.setCharacterStream(3, br, (int) text1.length());
//            ps.setBinaryStream(3, br, (int)text1.length());
            
            ps.executeUpdate();
            con.commit();
            System.out.println("插入大数据成功!");
            ps.close();
            stmt.close();
            con.close();            
        } catch (Exception e) {            
            e.printStackTrace();
        }
    }
    public void queryCBlob()
    {
        Connection con = getConn();
        BufferedWriter bw = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = con.prepareStatement("select tname,tcontent from tsblob");
            rs = ps.executeQuery();
            if(rs.next()){
                System.out.println(rs.getString(2));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值