一种向数据库中写入数据的方法
今天在我的博客群中有个朋友问了我一个问题,详见http://group.bokee.com/group/subject.7861.11763421854970771.html?91391218870400970
他问:怎样把文本文件中的每一列写到数据库表中?
我的解决方案如下:
开发工具:Eclipse3.2 JDK1.6 MySQL4.1.8
首先我打开MySQL命令行窗口,输入密码root后执行如下语句建立测试表student;
mysql
>
use
test;
mysql > create table student(id int ,name varchar ( 10 ));
mysql > create table student(id int ,name varchar ( 10 ));
我的文本文件(E:\data.txt,E盘为我的Java程序所在的根目录)的格式如下:
------------------------
1,Tom
2,Kate
3,Jim
-----------------------
对了!还有提醒大家一句:一定要把数据库驱动程序添加到Eclipse工程的JavaBuildpath中,呵呵,对于有经验的程序员来讲,我这纯属是废话,好了,不废话了,看看我的Java程序吧!
*
@author 我为J狂 建立日期
2007
-
4
-
14
*
*/
package net.blogjava.lzqdiy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
public class Test
{
/** *//**
* @param args
*/
public static Set<String> readFile(String filePath)
{
BufferedReader in = null;
Set<String> tableData = new HashSet<String>();
try
{
in = new BufferedReader(new FileReader(filePath));
String record = null;
while ((record = in.readLine()) != null)
{
tableData.add(record);
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try
{
in.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return tableData;
}
public static void insertDataBase(Set<String> tableData,String driverName,String url,String username,String password)
{
Connection con = null;
Statement stmt = null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(
url, username, password);
stmt = con.createStatement();
for (String record : tableData)
{
if(record!=null)
{
String[] values = record.split(",");
stmt.executeUpdate("insert into student(id,name) values(" + values[0]+ ",'" + values[1] + "')");
}
}
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if (stmt != null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if (con != null)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
}
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
insertDataBase(readFile(File.separator+"data.txt"),"com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/test", "root", "root");
}
}
*
*/
package net.blogjava.lzqdiy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
public class Test
{
/** *//**
* @param args
*/
public static Set<String> readFile(String filePath)
{
BufferedReader in = null;
Set<String> tableData = new HashSet<String>();
try
{
in = new BufferedReader(new FileReader(filePath));
String record = null;
while ((record = in.readLine()) != null)
{
tableData.add(record);
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try
{
in.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return tableData;
}
public static void insertDataBase(Set<String> tableData,String driverName,String url,String username,String password)
{
Connection con = null;
Statement stmt = null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(
url, username, password);
stmt = con.createStatement();
for (String record : tableData)
{
if(record!=null)
{
String[] values = record.split(",");
stmt.executeUpdate("insert into student(id,name) values(" + values[0]+ ",'" + values[1] + "')");
}
}
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if (stmt != null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if (con != null)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
}
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
insertDataBase(readFile(File.separator+"data.txt"),"com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/test", "root", "root");
}
}
我的这个程序只是能实现功能,在扩展性方面做得还不够,希望大家多提宝贵意见,我不胜感激。
另外,我感觉这种数据库输入数据的策略有利有弊,它的优点是快速方便,当记录内容的差别不大时,可以使用复制粘贴的方法快速更改文本文件的内容,然后执行Java程序一切都OK了;它的缺点也很明显就是灵活性较差,文本文件的中的每行记录中的数据必须用统一的分隔符来划分每个字段值(在我文本文件中用的是","),一旦用户将分隔符写错,那么Java程序就会失效。
|
|