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. ...
随机推荐
- Babel下的ES6兼容性与规范
前端开发 Babel下的ES6兼容性与规范 ES6标准发布后,前端人员也开发渐渐了解到了es6,但是由于兼容性的问题,仍然没有得到广泛的推广,不过业界也用了一些折中性的方案来解决兼容性和开发体系问 ...
- 我的SpringMVC配置
记住所有导的包都在org.springframework.web.servlet.mvc.annotation.下而不是 org.springframework.web.protlet.mvc.ann ...
- C#如何实现下载文件保存到本地上面去
public void btnTemplate_Click(object sender, EventArgs e) { string strResult = string.Empty; string ...
- FTF登入tiny210开发板
配置环境一: 第一步:安装虚拟机 1)安装虚拟机+Linux12.04 2)安装FTP软件 第二步:配置超级终端(串口) 1)开 ...
- MySQL的create table as 与 like区别
对于MySQL的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢? ? 1 create table t2 as select * fro ...
- c++中函数中变量内存分配以及返回指针、引用类型的思考
众所周知,我们在编程的时候经常会在函数中声明局部变量(包括普通类型的变量.指针.引用等等). 同时,为了满足程序功能的需要,函数的返回值也经常是指针类型或是引用类型,而这返回的指针或是引用也经常指向函 ...
- 关于模拟http请求 cookie的赋值
最近的工作一直是关于模拟http请求方面的知识的. 原本以为很简单,就是简单模拟一下http请求.先用fiddler模拟一下请求,验证接口可用,就直接上代码. 但是在模拟一个联通http的请求时候,我 ...
- [ASM C/C++] C语言的main 函数
C语言有两种可能的运行环境 1. 独立(freestanding) 在独立环境中,C程序执行不需要操作系统的支持,因此只具有最小的链接库能力. 2. 宿主(hosted) 在宿主的环境中,C程序会在操 ...
- 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)
参数说明: 'amount' : '10', //每次显示记录数 'address' : 'comments.php', //请求后台的地址 'format' : 'json', //数据传输格式 ' ...
- python中的 __slots__
__slots__的作用是防止给class instance分配dict,达到控制instance 成员和节省内存的作用 class Slots(object): __slots__=("n ...