JDBC练习
Posted m0_56426304
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC练习相关的知识,希望对你有一定的参考价值。
* 练习:
* 需求:
1. 通过键盘录入用户名和密码
2. 判断用户是否登录成功
* select * from user where username = "" and password = "";
* 如果这个sql有查询结果,则成功,反之,则失败 * 步骤:
1. 创建数据库表 user
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32),
PASSWORD VARCHAR(32)
);
INSERT INTO USER VALUES(NULL,'zhangsan','123');
INSERT INTO USER VALUES(NULL,'lisi','234');
2. 代码实现:
public class JDBCDemo9
public static void main(String[] args)
//1.键盘录入,接受用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
//2.调用方法
boolean flag = new JDBCDemo9().login(username, password);
//3.判断结果,输出不同语句
if(flag)
//登录成功
System.out.println("登录成功!");
else
System.out.println("用户名或密码错误!");
/**
* 登录方法
*/
public boolean login(String username ,String password)
if(username == null || password == null)
return false;
//连接数据库判断是否登录成功
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//1.获取连接
try
conn = JDBCUtils.getConnection();
//2.定义sql
String sql = "select * from user where username = '"+username+"' and password = '"+password+"' ";
//3.获取执行sql的对象
stmt = conn.createStatement();
//4.执行查询
rs = stmt.executeQuery(sql);
//5.判断
/* if(rs.next())//如果有下一行,则返回true
return true;
else
return false;
*/
return rs.next();//如果有下一行,则返回true
catch (SQLException e)
e.printStackTrace();
finally
JDBCUtils.close(rs,stmt,conn);
return false;
执行DDL操作:
package com.itheima;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 创建一张学生表
*/
public class Demo4DDL
public static void main(String[] args)
//1. 创建连接
Connection conn = null;
Statement statement = null;
try
conn = DriverManager.getConnection("jdbc:mysql:///day24", "root", "root");
//2. 通过连接对象得到语句对象
statement = conn.createStatement();
//3. 通过语句对象发送 SQL 语句给服务器
//4. 执行 SQL
statement.executeUpdate("create table student (id int PRIMARY key auto_increment, " +
"name varchar(20) not null, gender boolean, birthday date)");
//5. 返回影响行数(DDL 没有返回值)
System.out.println("创建表成功");
catch (SQLException e)
e.printStackTrace();
//6. 释放资源
finally
//关闭之前要先判断
if (statement != null)
try
statement.close();
catch (SQLException e)
e.printStackTrace();
if (conn != null)
try
conn.close();
catch (SQLException e)
e.printStackTrace();
执行DML操作:
package com.itheima;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 向学生表中添加 4 条记录,主键是自动增长
*/
public class Demo5DML
public static void main(String[] args) throws SQLException
// 1) 创建连接对象
Connection connection = DriverManager.getConnection("jdbc:mysql:///day24", "root",
"root");
// 2) 创建 Statement 语句对象
Statement statement = connection.createStatement();
// 3) 执行 SQL 语句:executeUpdate(sql)
int count = 0;
// 4) 返回影响的行数
count += statement.executeUpdate("insert into student values(null, '孙悟空', 1, '1993-03-
24')");
count += statement.executeUpdate("insert into student values(null, '白骨精', 0, '1995-03-
24')");
count += statement.executeUpdate("insert into student values(null, '猪八戒', 1, '1903-03-
24')");
count += statement.executeUpdate("insert into student values(null, '嫦娥', 0, '1993-03-
11')");
System.out.println("插入了" + count + "条记录");
// 5) 释放资源
statement.close();
connection.close();
执行DDL的操作:
package com.itheima;
import java.sql.*;
11 / 21
/**
* 查询所有的学生信息
*/
public class Demo6DQL
public static void main(String[] args) throws SQLException
//1) 得到连接对象
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/day24","root","root");
//2) 得到语句对象
Statement statement = connection.createStatement();
//3) 执行 SQL 语句得到结果集 ResultSet 对象
ResultSet rs = statement.executeQuery("select * from student");
//4) 循环遍历取出每一条记录
while(rs.next())
int id = rs.getInt("id");
String name = rs.getString("name");
boolean gender = rs.getBoolean("gender");
Date birthday = rs.getDate("birthday");
//5) 输出的控制台上
System.out.println("编号:" + id + ", 姓名:" + name + ", 性别:" + gender + ", 生日:" +
birthday);
//6) 释放资源
rs.close();
statement.close();
connection.close();
以上是关于JDBC练习的主要内容,如果未能解决你的问题,请参考以下文章
运动学基于matlab嫦娥奔月仿真含Matlab源码 1238期
嫦娥应悔偷灵药, 碧海青天夜夜心-《唐诗三百首》数据源API应用程序接口