关于JDBC+mysql中的longtext写入和读取问题 各位用过mysql中的longtext或者text的大神,小弟最近在学习使用ewebEditer和fckediter这种在线网页编辑工具,编辑完了会生成一串html源代码,我如何操作这些源代码,将其放入到我的数据库中呢??有实现过的吗?最好给个例子哈,给一小段代码也行,谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用法和varchar的没什么大区别吧,将html源码转成字符串直接写入数据库就是,我一般用PreparedStatement类的setString和getString方法 给你个例子你看看就是了啊连接数据库package bao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;interface Lian { public String DBDRIVER = "com.mysql.jdbc.Driver"; //public String DBDRIVER2 = "org.git.mm.mysql.Driver";//设置环境变量方法连接数据库 public String DBURl = "jdbc:mysql://localhost:3306/test"; public String DBUSER = "root"; public String DBPASSWD = "313094983";//填写你的密码 public Connection getConn();};public class LianJdbc implements Lian { private Connection conn = null; public LianJdbc() { try { Class.forName(DBDRIVER); } catch (Exception e1) { e1.printStackTrace(); } try { conn = DriverManager.getConnection(DBURl, DBUSER, DBPASSWD); } catch (SQLException e) { e.printStackTrace(); } } public Connection getConn() { return this.conn; }};longtextpackage bao;import java.sql.*;import java.util.Scanner;import java.io.*;class Clob extends LianJdbc{ private PreparedStatement pstm = null; //private ResultSet rs = null; public void insertClob() throws Exception{ Connection conn = null; conn = super.getConn(); String sql = "insert into userclob(name,note) values(?,?)"; this.pstm = conn.prepareCall(sql); File f = new File("d:"+File.separator+"1.txt"); InputStream is = new FileInputStream(f); this.pstm.setString(1, "傲慢与偏见"); this.pstm.setAsciiStream(2, is, (int)f.length()); this.pstm.executeUpdate(); this.pstm.close(); conn.close(); } public void selectClob() throws Exception{ Connection conn = super.getConn(); String sql = "select name,note from userclob where id = ?"; this.pstm = conn.prepareStatement(sql); this.pstm.setInt(1, 1); ResultSet rs = this.pstm.executeQuery(); if(rs.next()){ String name = rs.getString(1); StringBuffer sb = new StringBuffer(); System.out.println(name); InputStream is = rs.getAsciiStream(2); Scanner cin = new Scanner(is); cin.useDelimiter("\r\n"); while(cin.hasNext()){ sb.append(cin.next()).append("\n"); } System.out.println(sb); is.close(); } this.pstm.close(); conn.close(); }};class Blob extends LianJdbc{ private Connection conn = null; private PreparedStatement pstm = null; //private ResultSet rs = null; public void insertBlob() throws Exception{ this.conn = super.getConn(); String sql = "insert into userblob(id,name,photo) values(?,?,?)"; this.pstm = conn.prepareStatement(sql); this.pstm.setInt(1, 1); this.pstm.setString(2, "李冰冰"); File f = new File("d:"+File.separator+"2.gif"); InputStream is = new FileInputStream(f); this.pstm.setBinaryStream(3, is, f.length()); this.pstm.executeUpdate(); this.pstm.close(); conn.close(); }}public class CloBdemo{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Blob b = new Blob(); try { b.insertBlob(); } catch (Exception e) { e.printStackTrace(); } }} jsp页面提出的请求 到后台哪里接受请求 再问一个关于线程的问题! 请教一下如何在eclipse调试中如何知道一个变量什么时候变成NULL了,感谢 java简单问题 谁来帮我解决下?谢谢! 我按照书上打的,为什么报错?帮忙! 哪位可以讲讲编写ftp客户端的下载函数(方法) 要从word/excel表格中提取信息到数据库中,有什么建议? 过了SCJP和没过SCJP的都请进来,谢谢! 菜单栏怎么添加监听器代码 javamail 读取邮件内容时为什么邮件中的html内容读取不到? 求一个结果的原因
连接数据库package bao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;interface Lian {
public String DBDRIVER = "com.mysql.jdbc.Driver";
//public String DBDRIVER2 = "org.git.mm.mysql.Driver";//设置环境变量方法连接数据库
public String DBURl = "jdbc:mysql://localhost:3306/test";
public String DBUSER = "root";
public String DBPASSWD = "313094983";//填写你的密码 public Connection getConn();
};public class LianJdbc implements Lian {
private Connection conn = null; public LianJdbc() {
try {
Class.forName(DBDRIVER);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
conn = DriverManager.getConnection(DBURl, DBUSER, DBPASSWD);
} catch (SQLException e) {
e.printStackTrace();
}
} public Connection getConn() {
return this.conn;
}
};
longtextpackage bao;
import java.sql.*;
import java.util.Scanner;
import java.io.*;class Clob extends LianJdbc{
private PreparedStatement pstm = null;
//private ResultSet rs = null;
public void insertClob() throws Exception{
Connection conn = null;
conn = super.getConn();
String sql = "insert into userclob(name,note) values(?,?)";
this.pstm = conn.prepareCall(sql);
File f = new File("d:"+File.separator+"1.txt");
InputStream is = new FileInputStream(f);
this.pstm.setString(1, "傲慢与偏见");
this.pstm.setAsciiStream(2, is, (int)f.length());
this.pstm.executeUpdate();
this.pstm.close();
conn.close();
}
public void selectClob() throws Exception{
Connection conn = super.getConn();
String sql = "select name,note from userclob where id = ?";
this.pstm = conn.prepareStatement(sql);
this.pstm.setInt(1, 1);
ResultSet rs = this.pstm.executeQuery();
if(rs.next()){
String name = rs.getString(1);
StringBuffer sb = new StringBuffer();
System.out.println(name);
InputStream is = rs.getAsciiStream(2);
Scanner cin = new Scanner(is);
cin.useDelimiter("\r\n");
while(cin.hasNext()){
sb.append(cin.next()).append("\n");
}
System.out.println(sb);
is.close();
}
this.pstm.close();
conn.close();
}
};class Blob extends LianJdbc{
private Connection conn = null;
private PreparedStatement pstm = null;
//private ResultSet rs = null;
public void insertBlob() throws Exception{
this.conn = super.getConn();
String sql = "insert into userblob(id,name,photo) values(?,?,?)";
this.pstm = conn.prepareStatement(sql);
this.pstm.setInt(1, 1);
this.pstm.setString(2, "李冰冰");
File f = new File("d:"+File.separator+"2.gif");
InputStream is = new FileInputStream(f);
this.pstm.setBinaryStream(3, is, f.length());
this.pstm.executeUpdate();
this.pstm.close();
conn.close();
}
}
public class CloBdemo{ /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Blob b = new Blob();
try {
b.insertBlob();
} catch (Exception e) {
e.printStackTrace();
}
}
}