小弟有个项目:
需要在程序里帮客户导入数据库,也就是在程序里执行sql脚本.(创建数据库,创建 表,插入数据)
而不用在DOS下 mysql> mysql -h localhost -u root -p admin < C:\1.sql最好是java语言的.
:)
万分感谢!
需要在程序里帮客户导入数据库,也就是在程序里执行sql脚本.(创建数据库,创建 表,插入数据)
而不用在DOS下 mysql> mysql -h localhost -u root -p admin < C:\1.sql最好是java语言的.
:)
万分感谢!
1.读取文件
2.jdbc执行sql
import java.io.IOException;
import java.io.InputStream;
public class test {
public test() {
try {
Process p=Runtime.getRuntime().exec("cmd /c dir d:\\dictionary.txt /t:a");
InputStream in=p.getInputStream();
StringBuffer out = new StringBuffer();
byte[] b = new byte[1024];
int n=0;
while ((n=in.read(b))!=-1){
out.append(new String(b, 0, n));
}
System.out.println(out.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new test();
}
}
就是说把sql文件读成一个String,
然后像执行insert,update这些语句一样吗
参数不是很懂
拼成一个String
然后调statement类的execute() 方法