急求java源码 读取.txt文本生成sql server数据库文件 15
文本格式:"POLONIUS,lordchamberlain."PERSONA,7,13,11,"HORATIO,friendtoHamlet."PERSONA,7,14...
文本格式:"POLONIUS, lord chamberlain. "
PERSONA,7,13,11,"HORATIO, friend to Hamlet."
PERSONA,7,14,12,"LAERTES, son to Polonius."
PERSONA,7,15,13,"LUCIANUS, nephew to the king."
PGROUP,8,16,20,"null"
PERSONA,9,17,14,"VOLTIMAND"
PERSONA,9,18,15,"CORNELIUS"
PERSONA,9,19,16,"ROSENCRANTZ"
PERSONA,9,20,17,"GUILDENSTERN"
PERSONA,9,21,18,"OSRIC"
GRPDESCR,10,22,19,"courtiers."
PERSONA,7,23,21,"A Gentleman"
PERSONA,7,24,22,"A Priest. "
PGROUP,8,25,26,"null"
PERSONA,9,26,23,"MARCELLUS"
PERSONA,9,27,24,"BERNARDO"
GRPDESCR,10,28,25,"officers."
PERSONA,7,29,27,"FRANCISCO, a soldier." 展开
PERSONA,7,13,11,"HORATIO, friend to Hamlet."
PERSONA,7,14,12,"LAERTES, son to Polonius."
PERSONA,7,15,13,"LUCIANUS, nephew to the king."
PGROUP,8,16,20,"null"
PERSONA,9,17,14,"VOLTIMAND"
PERSONA,9,18,15,"CORNELIUS"
PERSONA,9,19,16,"ROSENCRANTZ"
PERSONA,9,20,17,"GUILDENSTERN"
PERSONA,9,21,18,"OSRIC"
GRPDESCR,10,22,19,"courtiers."
PERSONA,7,23,21,"A Gentleman"
PERSONA,7,24,22,"A Priest. "
PGROUP,8,25,26,"null"
PERSONA,9,26,23,"MARCELLUS"
PERSONA,9,27,24,"BERNARDO"
GRPDESCR,10,28,25,"officers."
PERSONA,7,29,27,"FRANCISCO, a soldier." 展开
1个回答
展开全部
import java.sql.*;
import java.util.*;
import java.io.*;
public class DBImport
{
public static void main(String [] args)
{
try
{
execute(new File(args[0] + ".sql"));
}
catch(SQLException e)
{
while(e != null)
{
e.printStackTrace();
e.getNextException();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
private static void execute(File file)
throws IOException, SQLException
{
BufferedReader in = new BufferedReader(new FileReader(file));
String sql;
Connection conn = getConnection();
Statement stat = conn.createStatement();
while((sql = in.readLine()) != null)
{
stat.execute(sql);
}
in.close();
stat.close();
conn.close();
}
public static Connection getConnection()
throws IOException, SQLException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("mysql.dat");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
return
DriverManager.getConnection(props.getProperty("url"), props);
}
}
最后一块,就是getConnection()这个函数你自己根据所要连的数据库改一下吧
import java.util.*;
import java.io.*;
public class DBImport
{
public static void main(String [] args)
{
try
{
execute(new File(args[0] + ".sql"));
}
catch(SQLException e)
{
while(e != null)
{
e.printStackTrace();
e.getNextException();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
private static void execute(File file)
throws IOException, SQLException
{
BufferedReader in = new BufferedReader(new FileReader(file));
String sql;
Connection conn = getConnection();
Statement stat = conn.createStatement();
while((sql = in.readLine()) != null)
{
stat.execute(sql);
}
in.close();
stat.close();
conn.close();
}
public static Connection getConnection()
throws IOException, SQLException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("mysql.dat");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
return
DriverManager.getConnection(props.getProperty("url"), props);
}
}
最后一块,就是getConnection()这个函数你自己根据所要连的数据库改一下吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询