一、查询人员名单,按序号 姓名 性格(男或女) 民族(某族) 生日(年月日)输出

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练习题:格式化日期、性别;避免代码注入的更多相关文章

  1. 写给小白的JAVA链接MySQL数据库的步骤(JDBC):

    作为复习总结的笔记,我罗列了几个jdbc步骤,后边举个简单的例子,其中的try块请读者自行处理. /* * 1.下载驱动包:com.mysql.jdbc.Driver;网上很多下载资源,自己找度娘,此 ...

  2. Java链接MySQL数据库的用配置文件和不用配置文件的代码

    1.利用配置文件(db.properties)链接MySQL数据库 package tool; import java.io.FileInputStream;import java.sql.Conne ...

  3. 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 ...

  4. (1)JDBC基础-java链接mysql数据库

    怎么操作数据库: 1,通过客户端(比如mac的终端,或者sql pro等专业工具)登陆数据库服务器(mysql -u root -p) 2,编写sql语句 3,发生sql语句到数据库服务器执行. JD ...

  5. java链接mysql

    比喻不是很合适,但能凑合用 解释 javaweb链接数据步骤 加载JDBC驱动 Class.forName("com.mysql.jdbc.Driver);//加载JDBC驱动 提供链接数据 ...

  6. java链接mysql数据库

    package com.DateSystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S ...

  7. java链接mysql 中文乱码

    {转!} 背景: 由于最近在开发一个APP的后台程序,需要Java连接远程的MySQL数据库进行数据的更新和查询操作,并且插入的数据里有中文,在插入到数据库后发现中文都是乱码.网上查了很多教程,最后都 ...

  8. Java链接MySql数据库(转)

    import java.sql.*; public class JDBCTest { public static void main(String[] args){ // 驱动程序名 String d ...

  9. java 链接mysql

    import java.sql.*; public class ConnectSql { static final String JDBC_DRIVER = "com.mysql.jdbc. ...

随机推荐

  1. liunx 远程拷贝到本地

    此句在本地git bash 执行,就能拷贝远程的目录 scp -r  userName@remote:/var/www/views/log/*.* ~/Desktop

  2. 【转】Python练习,网络爬虫框架Scrapy

    一.概述 下图显示了Scrapy的大体架构,其中包含了它的主要组件及系统的数据处理流程(绿色箭头所示).下面就来一个个解释每个组件的作用及数据的处理过程. 二.组件 1.Scrapy Engine(S ...

  3. Linux_know

    Linux_know 在创建Linux分区时,一定要创建SWAP/根分区两个分区 Red Hat Linux 9中,系统默认的root用户对整个系统拥有完全的控制权. 当登录Linux时,一个具有唯一 ...

  4. JSON对象长度和遍历方法

    摘自博客 (http://caibaojian.com/json-length.html) 原文链接:http://caibaojian.com/json-length.html JSON数组有长度j ...

  5. C# date format 使用C#格式化时间

    DateTime dt = DateTime.Now; //    Label1.Text = dt.ToString();//2005-11-5 13:21:25 //    Label2.Text ...

  6. 在eclipse上跑hadoop的helloworld

    关于hadoop的用处什么我就不说了,在这里记录下在eclipse上第一次跑hadoop遇到的问题吧~ hadoop的安装我就不说啦,网上教程一大堆~我直接用的公司的Linux上的hadoop. ec ...

  7. 关于wamp5中(apache)设置虚拟主机

    找了很多文章,但是很多对于最新的apache都已经过时无法生效了. http://blog.csdn.net/yuluo727282752/article/details/6944359 这篇文章写得 ...

  8. SQL行转列+动态拼接SQL

    数据源       Name AreaName qty Specific 叶玲 1 60 1 叶玲 2 1 1 叶玲 6 1 0 叶玲 7 5 0 叶玲 8 1 1 显示效果: Name 1 2 8 ...

  9. MVC Core

    .NET Core 1.1 发布 文档下载资源汇总 https://www.microsoft.com/net/core#windowsvs2015 https://docs.microsoft.co ...

  10. c++ 注册类到 lua

    test.h: #ifndef __TEST_H__ #define __TEST_H__ class CTest { public: CTest(); ~CTest(); int getA(); v ...