求助各位大大java作业的问题,绝不是代笔啊,只是问几个问题
作业基本就是一个利用jdbc连接数据库并且操作数据的编程作业..因为之前没有接触过java,所以都是摸索着来的,这个是我实现的能够读配置文件连接数据库,并且把一个文件的内容导入到数据库中.代码贴在下面,本来我这个程序直接java db 就可以运行了, 可是老师要求运行程序的命令行是java-classpath .;mysql-connector-java-5.1.18-bin.jarPopulate db.properties student.txt vehicle.txt route.txt stop.txt zone.txt的格式
因为没有学过java,都是找类似资料来实现的,这个要求找不到相应的资料只能求助各位大大了~拜托拜托了!
还有就是我的代码只是实现了读一个文件进去,老师这个要求是一下子读5个文件进去,分别存到不同的表里,而且要求要用JDBC PreparedStatementconstruct来实现重复的语句...请问这个该怎么修改代码啊
祝路过各位大大一生平安啊!!!
package practice;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public static void main(String[]args){
String s;
int t = 0;
String[] sa=new String;
List data = new ArrayList();
try{
BufferedReader in =new BufferedReader(new FileReader("src/db.properties"));
while((s=in.readLine())!=null){
sa =s.substring(0);
t++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
/*connect the DB*/
String url = "jdbc:mysql://"+sa+":"+sa+"/"+sa;
String username = sa;
String password = sa;
Connection conn = null;
Statement stmt = null;
try{
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
}catch (SQLException e1) {
e1.printStackTrace();
}
/*put data in DB*/
File file = new File("D:\\HW3\\HW3\\data\\student.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(input);
String line = null;
String sql = null;
String info[] = null;
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);
try {
while((line = br.readLine())!= null){
info = line.split(",");
sql = sql = "insert into cc(A,B,C)values('"+ info +"','"+info+"','"+info+"')";
stmt.executeUpdate(sql);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
还有就是配置文件路径这个地方有个问题就是我用netbeans能执行的没有问题,可是我把文件拿出来用命令行执行就说我找不到db.properties了,我明明吧程序和db.properties都还有连接驱动都放到一个文件夹下了
本帖最后由 nan_jia 于 2012-11-14 17:04 编辑
BufferedReader in =new BufferedReader(new FileReader("src/db.properties"));
肯定不对
应该是
BufferedReader in =new BufferedReader(new FileReader("db.properties"));
File file = new File("D:\\HW3\\HW3\\data\\student.txt");
这些也有问题吧。。 回复 2# nan_jia
可是我已经在netbean上运行,没有问题啊....不过用命令行运行的时候就说找不到文件了..不知道为毛啊,我明明已经把文件放到当前目录下了...
还有就是我现在明白是应该利用java的命令行参数来将文件作为参数导入...可是查来查去也不知道具体该怎么搞,再一个问题就是把不同的txt文件的内容导入不同名称的表,如何用PreparedStatemente避免重复代码啊。
把代码发来,我给你整。。 回复 4# nan_jia ,
啊啊
感谢这位好心大大啊!!我现在已经实现了的功能是用命令行参数读入一个配置文件去连接数据库,然后在读一个文件将该文件的内容存入数据库已有的表里。可是我们老师要求的是读五个文件,然后将这五个文件分别存入不同的已存在的表里面?请问我存入数据那块的代码就要重复写五遍吗?下面是我全部代码import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
public class db {
public static void main(String[]args){
/*read the properties*/
String s;
int t = 0;
String[] sa=new String;
List data = new ArrayList();
try{
BufferedReader in =new BufferedReader(new FileReader(args));
while((s=in.readLine())!=null){
sa =s.substring(0);
t++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
/*connect the DB*/
String url = "jdbc:mysql://"+sa+":"+sa+"/"+sa;
String username = sa;
String password = sa;
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = DriverManager.getConnection(url, username, password);
}catch (SQLException e1) {
e1.printStackTrace();
}
/*put data in DB*/
File file = new File(args);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(input);
String line = null;
String sql = null;
String info[] = null;
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);
try {
while((line = br.readLine())!= null){
info = line.split(",");
sql = "insert into cc(A,B,C)values('"+ info +"','"+info+"','"+info+"')";
stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} 本帖最后由 nan_jia 于 2012-11-16 14:22 编辑
如果是作业,好像这样也可以。。。。。
预编译要这样写。
try {
sql = "insert into cc(A,B,C)values(?,?,?)";
stmt = conn.prepareStatement(sql);
while((line = br.readLine())!= null){
info = line.split(",");
stmt.setString(1,info);
stmt.setString(2,info);
stmt.setString(3,info);
//stmt.executeUpdate();
stmt.addBath();
}
stmt.exceuteBath();
} catch (Exception e) {
e.printStackTrace();
} 回复 6# nan_jia
哦哦~那我这种preparestatement的写法是不是没有防注入的功能啊?是不是得按照您这格式写才有防注入的功能?还有就是能解答下是不是我导入5个文件的话是和导入一个文件一样的操作呢?要重复写5边一样的代码吗? 5 个文件的话如果有规律写成循环好点,重复5次其实简单一点。 回复 8# nan_jia while((line = br.readLine())!= null){
info = line.split(",");
sql = "insert into student(studentID,slocation)values(? ,GeomFromText( ' POINT(? ?) ' ) )";
stmt = conn.prepareStatement(sql);
stmt.setString(1, info);
stmt.setString(2, info);
stmt.setString(3, info);
stmt.execute(); }为啥这个一直报错java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1)
insert into student(studentID,slocation)values(? ,GeomFromText( ' POINT(? ) ' ) )
stmt.setString(1, info);
stmt.setString(2, info +" "+info);
试试看。
页:
[1]