Java链接MySQL练习题:格式化日期、性别;避免代码注入
一、查询人员名单,按序号 姓名 性格(男或女) 民族(某族) 生日(年月日)输出


import java.sql.*;
import java.text.SimpleDateFormat; public class Hr { public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
Statement state=conn.createStatement();
String sql="select * from info";
ResultSet rs=state.executeQuery(sql); while(rs.next()){
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getBoolean(3)?"男"+"\t":"女"+"\t");//三目运算符 ?:
System.out.print(minZu(rs.getString(4))+"\t");//调用minZu方法,按民族代码查到民族名称
System.out.print(riQi(rs.getDate(5))+"\n");//调用riQi方法格式化日期
}
conn.close();
}
public static String riQi(Date d) {//新建一个方法riQi,格式化日期为xxxx年xx月xx日
SimpleDateFormat f=new SimpleDateFormat("yyyy年MM月dd日");
return f.format(d);
} public static String minZu(String mz) throws Exception{//新建一个方法
String mzmc="";
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
Statement state=conn.createStatement();
String sql="select * from nation where code='"+mz+"' ";//在nation表,按民族代码查到民族名称
ResultSet rs= state.executeQuery(sql);
if(rs.next()==true){
mzmc=rs.getString(2);
}
conn.close();
return mzmc; } }
输出结果
p001 胡军 男 满族 1985年08月09日
p002 周丹 女 汉族 1984年04月17日
p003 吴倩 女 苗族 1981年10月29日
p004 唐墨 男 汉族 1983年02月25日
二、输入账号、密码实现登陆
import java.sql.*;
import java.util.Scanner; public class Login {
public static void main(String[] args) throws Exception{
//输入账号密码
Scanner sc=new Scanner(System.in);
System.out.println("账号:");
String uid=sc.nextLine();
System.out.println("密码:");
String pwd=sc.nextLine(); uid=uid.replace("\'", "\"");//将用户输入的内容中单引号(')替换为双引号(")
/*如果输入内容存在单引号(')就会破坏sql语句的结构,例如uid=a' or 1=1 #
sql语句就变成了 select * from users where username='a' or 1=1 #' and password='"+pwd+"'
这样整段sql语句就会被破坏掉,这种方式叫做代码注入*/ //到数据库连接用户名密码正确与否
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK", "root", "");
Statement state=conn.createStatement();
String sql="select * from users where username='"+uid+"' and password='"+pwd+"'";
ResultSet rs=state.executeQuery(sql);
//输出
if(rs.next()){
System.out.println("登陆成功,欢迎"+rs.getString(3)); }else{
System.out.println("用户名或密码错误");
}
conn.close();
} }
推荐用PreparedStatement接口避免代码注入
public static void main(String[] args) throws Exception{
//输入账号密码
Scanner sc=new Scanner(System.in);
System.out.println("账号:");
String uid=sc.nextLine();
System.out.println("密码:");
String pwd=sc.nextLine();
//连接到数据库判断账号密码
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK","root","");
String sql="select * from users where username=? and password=? ";//语句避免出现单引号('),无论用户输入什么内容都不会造成代码注入
PreparedStatement state=conn.prepareStatement(sql);
state.setString(1, uid);//表示sql语句中的第一个“?”
state.setString(2, pwd);//与上同理
ResultSet rs=state.executeQuery();
//输出结果
if(rs.next()){
System.out.println("登陆成功,欢迎"+rs.getString(3));
}else{
System.out.println("用户名或密码错误");
}
conn.close();
}
三、往数据库表里插入新内容
public static void main(String[] args) throws Exception{
//输入数据
Scanner sc=new Scanner(System.in);
System.out.println("账号:");
String uid=sc.nextLine();
System.out.println("密码:");
String pwd=sc.nextLine();
System.out.println("昵称:");
String nick=sc.nextLine();
//连接数据库并处理
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK","root","");
String sql="insert into users values(?,?,?)";
PreparedStatement state=conn.prepareStatement(sql);
state.setString(1, uid);//数字1表示sql语句中第一个“?”
state.setString(2, pwd);
state.setString(3, nick);
state.executeUpdate();
conn.close();
}
Java链接MySQL练习题:格式化日期、性别;避免代码注入的更多相关文章
- 写给小白的JAVA链接MySQL数据库的步骤(JDBC):
作为复习总结的笔记,我罗列了几个jdbc步骤,后边举个简单的例子,其中的try块请读者自行处理. /* * 1.下载驱动包:com.mysql.jdbc.Driver;网上很多下载资源,自己找度娘,此 ...
- Java链接MySQL数据库的用配置文件和不用配置文件的代码
1.利用配置文件(db.properties)链接MySQL数据库 package tool; import java.io.FileInputStream;import java.sql.Conne ...
- java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
- (1)JDBC基础-java链接mysql数据库
怎么操作数据库: 1,通过客户端(比如mac的终端,或者sql pro等专业工具)登陆数据库服务器(mysql -u root -p) 2,编写sql语句 3,发生sql语句到数据库服务器执行. JD ...
- java链接mysql
比喻不是很合适,但能凑合用 解释 javaweb链接数据步骤 加载JDBC驱动 Class.forName("com.mysql.jdbc.Driver);//加载JDBC驱动 提供链接数据 ...
- java链接mysql数据库
package com.DateSystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S ...
- java链接mysql 中文乱码
{转!} 背景: 由于最近在开发一个APP的后台程序,需要Java连接远程的MySQL数据库进行数据的更新和查询操作,并且插入的数据里有中文,在插入到数据库后发现中文都是乱码.网上查了很多教程,最后都 ...
- Java链接MySql数据库(转)
import java.sql.*; public class JDBCTest { public static void main(String[] args){ // 驱动程序名 String d ...
- java 链接mysql
import java.sql.*; public class ConnectSql { static final String JDBC_DRIVER = "com.mysql.jdbc. ...
随机推荐
- hdu 1556
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Samba文件服务器详细配置步骤
准备安装 环境:CentOS 6.3_x64bit 安装:Minimal(最小) 1.配置IP地址 2.挂载:[root@localhost ~]# mount -t iso9660 /dev/cdr ...
- 夺命雷公狗----Git---3---vi编辑器
如果直接使用了 git commit 即进入vi编辑器,所以强烈推荐使用 git commit -m 中文注释 但是如果进入vi编辑器其实也没什么好怕的,如果动linux 的朋友应该都会使用 进入v ...
- 多线程迭代之——LINQ to TaskQuery
平时经常会迭代集合,如果数据多的话会很耗时. 例子: , , }; list.ForEach(a => DoSomething(a)); void DoSomething(int a) { // ...
- java代码优化
优化通常包含两方面的内容:减小代码的体积,提高代码的运行效率. 1.尽量指定类的final修饰符 带有final修饰符的类是不可派生的.在Java核心API中,有许多应用final的例子,例如java ...
- Material Design Lite,简洁惊艳的前端工具箱 之 交互组件。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, 博客地址为http://www.cnblogs.com/jasonnode/ . 网站上有对应 ...
- SQL2008完全卸载详解(图解)
一. SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板”
- Java数据结构之字符串模式匹配算法---KMP算法
本文主要的思路都是参考http://kb.cnblogs.com/page/176818/ 如有冒犯请告知,多谢. 一.KMP算法 KMP算法可以在O(n+m)的时间数量级上完成串的模式匹配操作,其基 ...
- Linux的常用命令
1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切 ...
- 【转】缺少servlet-api.jar包
转载地址:http://blog.sina.com.cn/s/blog_6cfb18070100n7pu.html 在Eclipse中缺省servlet-api.jar包,由于servlet-apbi ...