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. ...
随机推荐
- 使用C#创建快捷方式
在Windows中创建快捷方式很简单,直接用右键点击文件或文件夹,选择创建快捷方式即可.如果想用C#代码的方式创建,就没有那么方便了,因为.NET框架没有提供直接创建快捷方式的方法. 首先我们看一下快 ...
- 我的archlinux中安装的关于xfce4的软件
包括一些panel的插件: xfce4-appfinder - xfce4-battery-plugin - xfce4-eyes-plugin - xfce4-mixer - xfce4-netlo ...
- zw.delphi不同版本程序运行速度测试
{ zw.delphi不同版本程序运行速度测试 delphi无论是开发,编译,还是运行,速度方面向来不差,笔者很少进行这种微粒度的优化,调试. 最近,因为项目需要,发现:同一个函数模块,差不多同样的代 ...
- android 布局学习
各种layout用到的一些重要属性 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertic ...
- UNION和UNION ALL
UNION 用于合并两个或多个 SELECT 语句的结果集,并消去表中任何重复行.UNION 内部的 SELECT 语句必须拥有相同数量的列,列也必须拥有相似的数据类型.同时,每条 SELECT 语句 ...
- C# date format 使用C#格式化时间
DateTime dt = DateTime.Now; // Label1.Text = dt.ToString();//2005-11-5 13:21:25 // Label2.Text ...
- 突袭HTML5之WebGL 3D概述
WebGL开启了网页3D渲染的新时代,它允许在canvas中直接渲染3D的内容,而不借助任何插件.WebGL同canvas 2D的API一样,都是通过脚本操纵对象,所以步骤也是基本相似:准备工作上下文 ...
- post上传文件
- (BOOL)sendPhotoToTumblr:(NSString *)photo withCaption:(NSString *)caption; { //get image d ...
- 30分钟学会如何使用Shiro
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...
- javax mail网址
http://www.oracle.com/technetwork/java/javamail/faq/index.html#tomcatconfig