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. ...
随机推荐
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- 关于针对class自定义new操作符失败的函数处理
#include <iostream> #include <new> using namespace std; class CSaveCurHandler //用于管理new_ ...
- 2.1 C#的关键字
关键字是被C#规定了用途的重要单词.在visual studio中,关键字以蓝色字体显示.图中红色方框圈起来的就是关键字. 关键字 class ,这个关键字的用途是声明类.比如上面例子中,类名叫做Pr ...
- jQuery对json快速赋值
jQuery对json快速赋值,重点在于将input的id取跟JSON同样的名称. <!DOCTYPE html> <html> <head lang="en& ...
- YII2.0 Activeform表单组件的使用方法
Activeform文本框:textInput();密码框:passwordInput();单选框:radio(),radioList();复选框:checkbox(),checkboxList(); ...
- mysql 性能优化方案1
网 上有不少mysql 性能优化方案,不过,mysql的优化同sql server相比,更为麻烦与复杂,同样的设置,在不同的环境下 ,由于内存,访问量,读写频率,数据差异等等情况,可能会出现不同的结果 ...
- 原生态PHP+mysql 实现分页
<?php/** * 数据分页 * 时间:2016-07-06 *//**1传入页码**/ $page = $_GET['p'];/**2根据页码取数据:php->mysql处理**/$h ...
- Ring buffers and queues
Ring buffers and queues The data structure is extremely simple: a bounded FIFO. One step up from pla ...
- SAMSUNG某型号一千短信成功记录!对比其他软件恢复不成功的案列!
Hello! 大家好欢迎再次来到Dr.wonde的博客, 下面谈一下今天的案列,今年11月26号收到了一客户寄来的三星S4手机恢复里面短信, 如下图所示,用其他软件恢复以后,数据为零,没有恢复,,这下 ...
- make phpexcel working with XAMPP7.0.9
Environment XAMPP 7.0.9 (PHP 7.0.9) PHPExcel 1.7.6-1.8.1 not lib_zip.dll Windows 10.1 thinkPHP 5.0.1 ...