.txt保存阅读理解,java流读取各个单词,并将其数量存入数据库,然后查询数据库将结果写入.txt文件
package New;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.PreparedStatement;
public class English {
static File file = new File("C:\\Users\\57273\\Desktop\\English.txt");
static String name ="C:\\Users\\57273\\Desktop\\English.txt";
static String url=("jdbc:mysql://localhost:3306/date");
static String user = "root";
static String password = "root";
public static Connection Connect() throws SQLException
{
Driver driver = new com.mysql.jdbc.Driver();
Properties pro = new Properties();
pro.setProperty("user", user);
pro.setProperty("password", password);
Connection con = (Connection) driver.connect(url, pro);
System.out.println(con);
return con;
}
public static String SloveString(String string)
{
System.out.println(string);
char ch[] = new char [string.length()];
for(int i=0;i<string.length();i++)
{
if(string.charAt(i)>='a'&&string.charAt(i)<='z'||string.charAt(i)>='A'&&string.charAt(i)<='Z')
{
ch[i]=string.charAt(i);
}
else
ch[i]=' ';
}
return new String(ch);
}
public static void slove (File file) throws Exception
{
Connection con = Connect();
@SuppressWarnings("unused")
FileWriter writer;
try
{
String pathname = "C:\\Users\\57273\\Desktop\\English.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
BufferedReader buffer = new BufferedReader(reader);
String line = buffer.readLine();
if(line==null)
{
System.out.println("stupy");
}
PreparedStatement pstmt1=null;
while(line!=null)
{
System.out.println(line);
String line1 = SloveString(line);
System.out.println(line1);
String help[] = line1.split(" ");
for(int i=0;i<help.length;i++)
{
String sql10 = "insert into english values('"+help[i]+"',1)";
pstmt1=(PreparedStatement) con.prepareStatement(sql10);
pstmt1.executeUpdate();
/*String sql1="select *from English where word ='"+help[i]+"'";
pstmt1=(PreparedStatement) con.prepareStatement(sql1);
ResultSet rs=pstmt1.executeQuery();
if(rs.next())//如果已经有记录
{
String s = rs.getString("count");
int num = Integer.valueOf(s);
String st = String.valueOf(num);
String sql2 = "update English set count="+st+"where word="+rs.getString(help[i]);
pstmt1 = (PreparedStatement) con.prepareStatement(sql2);
pstmt1.executeUpdate();
}
else
{
String sql="INSERT INTO English VALUES ('" + help[i]+','+ "1 ')";
pstmt1 = (PreparedStatement) con.prepareStatement(sql);
pstmt1.executeUpdate();
}*/
}
line=buffer.readLine();
}
//writer = new FileWriter(file);
String MainSql="SELECT word,sum(count) as he from English GROUP BY word ORDER BY he DESC";
pstmt1=(PreparedStatement) con.prepareStatement(MainSql);
//pstmt1.executeUpdate();
ResultSet rs1=pstmt1.executeQuery();
String AnswerName = "C:\\Users\\57273\\Desktop\\Answer.txt";
File WriteFile = new File(AnswerName);
writer = new FileWriter(WriteFile);
int number=0;
while(rs1.next())
{
//System.out.println(rs1);
String answer = rs1.getString("word")+String.valueOf(rs1.getInt(2));
//String answer = String.valueOf(rs1.getInt("count"));
//int answer = rs1.getInt(2);
//System.out.println(rs1.getString("count"));
//System.out.println("1");
// System.out.println(answer+"23333");
//String answer = rs1.getString(number++);
//System.out.println(answer+"233333");
writer.write(answer);
writer.write("\r\n");
}
rs1.close();
writer.close();
}
catch(Exception e)
{
}
System.out.println("Hello World!");
con.close();
}
public static void main(String[] args) throws Exception {
slove(file);
/*File filename=new File(name);
InputStreamReader reader=new InputStreamReader(new FileInputStream(filename));
BufferedReader buffer=new BufferedReader(reader);
String line=buffer.readLine();
while(line!=null)
{
System.out.println(line);
line = buffer.readLine();
}*/
}
}
结果如下:
分析之后为:
时隔一年 又一次更新博客了,拦不住的考研算是失败了,不过 以后有机会 还是想提升一下自己。
又想起来写代码,发现写的还行,还没忘完,不知道现在这种操作 还能不能找到工作,工资多少都行,只要能学到东西都好。
努力,共勉!