哪位仁兄帮我看下,没有报错,但是在User1.java却没有得到数据.
public static void readText() throws ClassNotFoundException, SQLException, IOException{
Connection conn=null;
Statement st=null;;
ResultSet rs=null;
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="55667788";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
String sql="select content from classname";
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
Clob clob=rs.getClob(1);
Reader reader = clob.getCharacterStream();
File file=new File("src/User1.java");
Writer writer=new BufferedWriter(new FileWriter(file));
char[]buff=new char[1024];
for(int i=0;(i=reader.read(buff))>0;){
writer.write(buff, 0, i);
}
}
rs.close();
st.close();
conn.close();
}
public static void readText() throws ClassNotFoundException, SQLException, IOException{
Connection conn=null;
Statement st=null;;
ResultSet rs=null;
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="55667788";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
String sql="select content from classname";
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
Clob clob=rs.getClob(1);
Reader reader = clob.getCharacterStream();
File file=new File("src/User1.java");
Writer writer=new BufferedWriter(new FileWriter(file));
char[]buff=new char[1024];
for(int i=0;(i=reader.read(buff))>0;){
writer.write(buff, 0, i);
}
}
rs.close();
st.close();
conn.close();
}
2.对文件操作,用完也得关闭
请依次检查:
1.是否存在表及字段及内容(Clob的类型。
2.程序如果编译没问题,看下你的那个User1.java文件被创建了没。
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class Test { public static void main(String[] args) throws Exception {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "55667788";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select content from classname";
st = conn.createStatement();
rs = st.executeQuery(sql);
File file = new File("src/User1.java");
Writer writer = new BufferedWriter(new FileWriter(file));
while (rs.next()) {
writer.write(rs.getString(1));
writer.flush();
}
rs.close();
st.close();
conn.close();
}
}