今天实现了对数据库中新闻文本的读取
public class DBUtil {
private static String url = "jdbc:mysql://localhost:3306/xinwen?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
private static String user = "root";
private static String password = "0408";
private static String jdbcName="com.mysql.jdbc.Driver";
private Connection con=null;
public static Connection getConnection() {
Connection con=null;
try {
Class.forName(jdbcName);
con=DriverManager.getConnection(url, user, password);
//System.out.println("数据库连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
//System.out.println("数据库连接失败");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url,user,password);
System.out.println("连接成功");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
public static void main(String[] args)throws SQLException {
Bean bean = new Bean();
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql ="select content from xilang2";
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
System.out.println(getConnection());
String content;
rs.last();// 移动到最后
int n=rs.getRow();
System.out.println(rs.getRow());// 获得结果集长度
rs.beforeFirst();
String[] data=new String[n];
int j=0;
while(rs.next()){
data[j]=rs.getString("content");
System.out.println(data[j]);
j++;
}
Connection conn2 = getConnection();
Statement pstmt2 = null;
pstmt2=conn.createStatement();
for(int i=0;i<data.length;i++) {
content = data[i];
System.out.println(content);
String type=BayesClassifier.getResult(content);
System.out.println(type);
String sql2="update xilang2 set type='"+type+"' where content='"+content+"'";
pstmt2.execute(sql2);
}
/*try {
while(rs.next())
{
String contant = rs.getString("contant");
System.out.println(contant);
}
}catch(SQLException e){
e.printStackTrace();
}*/
// return con;
}
public static void close(Connection con) {
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement state, Connection conn) {
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement state, Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}