package _pasted_code_;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java. sql.*;public class Conn {static Connection con;static Statement sql;static ResultSet res;public Connection getConnection(){try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");System.out.println("Database loading succeeded.");}catch(ClassNotFoundException e){e.printStackTrace();}try{con=DriverManager.getConnection("jdbc:sqlserver://172.16.13.150:1433;DatabaseName=zyl_test","sa","11111111");System.out.println("Database connnection succeeded.");}catch(SQLException e){e.printStackTrace();}return con;}public static void main(String[] args){Conn c=new Conn();c.getConnection();try {FileInputStream fin = new FileInputStream("D:/输入输出.txt");InputStreamReader inR = new InputStreamReader(fin);BufferedReader bfR = new BufferedReader(inR);String temp = "";String[] tempArr = {};String name="";int age;String add="";String[] Temp = {};while((temp=bfR.readLine())!=null){tempArr = temp.split(","); name = tempArr[0];age = Integer.parseInt(tempArr[1]);add = tempArr[2];try{sql=con.createStatement();//System.out.println("insert test1 values ('"+name+"',"+age+",'"+add+"')");sql.execute("insert test1 values ('"+name+"',"+age+",'"+add+"')");}catch(Exception e){e.printStackTrace();}}} catch (FileNotFoundException e1) {// TODO 自动生成的 catch 块e1.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}try{sql=con.createStatement();res=sql.executeQuery("select * from test1");while(res.next()){String name=res.getString("name");System.out.println(name);}}catch(Exception e){e.printStackTrace();}}}
【JAVA】【SQL】一个demo(将文件中的数据取出来放到数据库中)
最新推荐文章于 2023-10-31 08:14:51 发布