java关键字查询数据库

 1 package com.ningmeng;
 2 
 3 import java.sql.*;
 4 
 5 public class Test07 {
 6 
 7     public static void main(String[] args) throws Exception {
 8             Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
 9             System.out.println("加载数据库驱动成功");
10             String url="jdbc:mysql://localhost:3306/mldn";//声明自己的数据库test的url
11             String user="root";//自己的数据库用户名
12             String pass="root";//自己的数据库密码
13             //建立数据库连接,获得连接的对象conn
14             Connection conn=DriverManager.getConnection(url,user,pass);
15             System.out.println("连接数据库驱动成功");
16             
17             Statement stmt=conn.createStatement();//创建一个Statement对象
18             String sql="select * from emp where empno like '%125%' ";//生成sql语句
19             ResultSet rs=stmt.executeQuery(sql);//执行sql语句           
20             
21             int empno;
22             String ename=null,job;
23             java.util.Date hiredate;
24             float sal;
25             
26             System.out.println("empno	 ename	 job");
27             
28             while(rs.next()){
29                 empno=rs.getInt("empno");
30                 ename=rs.getString(2);
31                 job=rs.getString("job");
32                 hiredate=rs.getDate(4);
33                 sal=rs.getFloat(5);
34                 System.out.println(empno + "	" + ename + "	" + job + 
35                         "	" +hiredate + "	" + sal);//输出查询结果
36             }
37             
38             //判断所查对象是否存在
39             if(ename == null){
40                  System.out.println("false");
41             }else{
42                  System.out.println(true);
43             }
44             
45             System.out.println("模糊查询成功");
46             conn.close();//关闭数据库连接
47             System.out.println("关闭数据库连接成功");
48     }
49 }
数据库如下图所示:

原文地址:https://www.cnblogs.com/XuGuobao/p/7138586.html