DB 从zl.xml中导入数据库用户名及密码等!

package com.dy.java;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DB
{
    
    private String ip="127.0.0.1";
    private String port="1433";
    private String url ="jdbc:sqlserver://"+ip+":"+port+";"+"DatabaseName=txl";
    private static String user="";
    private static String password="";
    private static String str="";
    private Connection con;

    private String sql;
    private PreparedStatement sta;

    private static void init()
    {
        try
        {
            InputStream in =new FileInputStream("E:/zl.xml");
            byte[] b =new byte[1024];
            int y;
            while((y=in.read(b))!=-1)
            {
                str+= new String(b,0,y);    
            }
//            System.out.println(str);
            if(str!=null&&str!=""&&str.length()!=0)
            {
                int n =str.indexOf("<user>")+"<user>".length();
                int m =str.indexOf("</user>");
                user =str.substring(n, m);
//                System.out.println(user);
                int n1 =str.indexOf("<password>")+"<password>".length();
                int m1=str.indexOf("</password>");
                password =str.substring(n1, m1);
//                System.out.println(password);
                
            }
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    
    public DB()
    {    
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con=DriverManager.getConnection(url,user,password);            
        } catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void go()
    {
        try
        {
            sta=con.prepareStatement(sql);
            sta.execute("insert into txl() values ()");
        } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    
    public static void main(String[] args)
    {
        DB.init();
        DB db =new DB();
//        db.go();
    }

}

zl.xml

<url>jdbc:sqlserver://</url>
<ip>127.0.0.1</ip>
<port>1433</port>
<user>sa</user>
<password>520</password>
_____________________________________________________________________________________________________ ***************************************************************************************************************** ===坚持、奋斗***今天的努力只为更好的明天***奋斗、坚持===********************** ==========================================================================================
原文地址:https://www.cnblogs.com/mlloc-clove/p/3565649.html