ReadConfigureFile.java
package ToMySQLDB;
import java.io.BufferedReader;import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class ReadConfigureFile {
ReadConfigureFile(String tabelName,String filePath) throws IOException,SQLException{
//创建数据库连接对象;调用同一个包中类DBUtil中的getConnection()方法
Connection conn=DBUtil.getConnection();
//创建文件对象,filePath是文件的路径名称
File file=new File(filePath);
//在给定读取数据文件的情况下创建一个新的FileReader对象,FileReader对象是以字符流读取数据的而不是字节流。
FileReader fr=new FileReader(file);
//创建一个使用默认大小输入缓冲区的缓冲字符输入流(对象)
BufferedReader br=new BufferedReader(fr);
String line;
//声明int类型变量count;用于记录向数据库中添加数据的条数
int count=0;
String info[] = null;
try {
//得到读取文件的全路径 E:\rfp\rfpFile\安徽联通.txt
String path = file.getAbsolutePath();
//得到文件名:安徽联通
String fileName = path.substring(path.lastIndexOf("\\")+1, path.lastIndexOf("."));
//获取子字符串
String province = fileName.substring(0,fileName.length()-2);
String cardType = fileName.substring(fileName.length()-2,fileName.length());
while((line = br.readLine())!= null){
//分割字符串,以"-"为分隔符
info = line.split("-");
String sql = "insert into "+tabelName+"(startPhone,endPhone,city,province,cardType) values('"+info[0]+"','"+info[1]+"','"+info[2]+"','"+province+"','"+cardType+"')";
Statement stmt=null;
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("sql:"+sql);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException,SQLException{
ReadConfigureFile rcf=new ReadConfigureFile("phonenumber","E:\\rfp\\rfpFile\\安徽联通.txt");
}
}
DBUtil .java
package file;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//JDBC访问数据库步骤:1、加载注册驱动。在Java程序中需要使用第三方(非JDK内置)的jar包,把软件包添加到程序的classpath(类路径)中。
//在Eclipse中添加软件包的方法与项目类型有关。 Java Web项目,直接将JDBC驱动的Jar文件复制到项目的web—inf/lib/下即可。
public class DBUtil {
public static Connection getConnection(){
//url:连接数据库的字符串。通常url中包含数据库的ip,端口,库名等。这里最后的test是我的数据库名。
String url="jdbc:mysql://localhost/test";
//连接数据库的用户名
String username="root";
//连接数据库的密码
String password="sa123";
Connection conn=null;
try {
try {
//JDBC访问数据库步骤:2、实例化数据库连接驱动类,并将其注册到DriverManager中。
//JDBC驱动包添加完成后在程序中注册驱动的方法如下1、2两行,driver参数是JDBC驱动类的完整类名称
String dirver="com.mysql.jdbc.Driver";//1
//Java.lang.class类的forname方法可以根据类名初始化类实例,从而实现JDBC驱动类注册。
Class.forName(dirver);//2
//JDBC访问数据库步骤:3、建立数据库连接。建立数据库连接时通过java.sql.DriverManager类实现的,代码示例
conn=DriverManager.getConnection(url, username, password);
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
DBUtil db=new DBUtil();
Connection connection=db.getConnection();
try {
//判断数据库是否连接成功
if(!connection.isClosed())
{
System.out.println("Successed connecting to database");
}else
{
System.out.println("Failed connecting to database");
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(connection.getClass().getName());
}
}
界面显示结果如图:
1、数据库中未插入任何数据时,建好了数据库test,表名phonenumber,属性startPhone,endPhone,city,province,cartType
2、要插入数据库的数据文本:安徽联通.txt
3、程序的运行结果:
4、数据库插入数据之后的界面显示:
PS:只简单把文本文件内容添加到数据库的链接:http://blog.csdn.net/u013003837/article/details/28892401
我把 源码项目和文本已经上传了,链接地址:http://download.csdn.net/detail/u013003837/7458751