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>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。