黑马程序员--手机号码归属地验证

本文介绍了一种从文件中批量导入电话号码区域数据到数据库的方法,并提供了验证数据正确性的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------


数据库数据可以在百度GOOGLE搜一下,有很多。优快云上也有

将数据导入数据库


        private void button1_Click(object sender, EventArgs e)
        {
            string phoneNo,province,city,areacode,postcode,phoneNoType;
            string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            { return; }
            //using(FileStream filestream=File .OpenRead(openFileDialog1.FileName))
            //{
                using (StreamReader streamreader = new StreamReader(openFileDialog1.FileName,Encoding.Default))
                {

                    using (SqlConnection conn = new SqlConnection(connstr))
                    {
                        conn.Open();
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "insert into phoneNoArea(phoneNo,province,city,areacode,postcode,phoneNoType) values(@phoneNo,@province,@city,@areacode,@postcode,@phoneNoType)";
                            string line = null;
                            while ((line = streamreader.ReadLine()) != null)
                            {
                                MatchCollection mc = regex.Matches(line);
                                string[] strarr = line.Split('-');
                                phoneNo = strarr[1];
                                province = strarr[2];
                                city = strarr[3];
                                areacode = strarr[4];
                                postcode = strarr[5];
                                phoneNoType = strarr[6];
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("phoneNo", phoneNo);
                                cmd.Parameters.Add("province", province);
                                cmd.Parameters.Add("city", city);
                                cmd.Parameters.Add("areacode", areacode);
                                cmd.Parameters.Add("postcode", postcode);
                                cmd.Parameters.Add("phoneNoType", phoneNoType);
                                cmd.ExecuteNonQuery();

                            }
                        }
                    }
                //}
            }
            MessageBox.Show("导入成功");
        }



以下是验证数据的代码

 private void button2_Click(object sender, EventArgs e)
        {
            string phoneNostr = textBox1.Text.Substring(0,7);
            string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT [PhoneNo],[Province],[City],[AreaCode],[PostCode],[PhoneNoType] FROM [test].[dbo].[PhoneNoArea] where [PhoneNo] =@phoneNo";
                    cmd.Parameters.AddWithValue("phoneNo", phoneNostr);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())//读取数据,并判断是否为真
                        {
                            string result = reader.GetString(reader.GetOrdinal("Province"));
                            result += " " + reader.GetString(reader.GetOrdinal("city"));
                            result += " " + reader.GetString(reader.GetOrdinal("phoneNoType"));
                            resultlabel1.Text = result;
                        }
                    }
                    
                }
            }
        }

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------详细请查看: http://net.itheima.com/
### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置类来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值