zoukankan      html  css  js  c++  java
  • 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>
    _____________________________________________________________________________________________________ ***************************************************************************************************************** ===坚持、奋斗***今天的努力只为更好的明天***奋斗、坚持===********************** ==========================================================================================
  • 相关阅读:
    《基于玩家分享行为的手游传播模式研究》
    并行多核体系结构基础——第四章知识点和课后习题
    numpy中的nan和常用方法
    《基于多层复杂网络的传播行为建模与分析》
    《基于SD-SEIR模型的实验室人员不安全行为传播研究》
    《基于SIR的路边违停行为传播模型研究》
    《基于SIRS模型的行人过街违章传播研究》
    阿里巴巴编码规范-考试认证
    测试菜鸟!!当领导我问:“测得怎么样了?”我慌到一P
    国内软件测试过度吹捧自动化测试,然而在国外是这样子的
  • 原文地址:https://www.cnblogs.com/mlloc-clove/p/3565649.html
Copyright © 2011-2022 走看看