zoukankan      html  css  js  c++  java
  • 数据库查找

    import java.sql.*;

     
    import java.util.Scanner;
     
    public class jdbcselect {
        public static void main(String[]args) {
            final String URL = "jdbc:mysql://localhost:3306/test";
            final String USERNAME = "root";
            final String PWD = "12345";
            Connection connection = null;
            Statement  stmt = null;
            ResultSet rs = null ;
            Scanner con=new Scanner(System.in);
            String idnumber;
            String classname;
            String teachername;
            String didian;
     
            teachername=con.nextLine();
            /*teachername=con.nextLine();
            didian=con.nextLine();*/
            try {
                // a.导入驱动,加载具体的驱动类
                Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
                // b.与数据库建立连接
                connection = DriverManager.getConnection(URL, USERNAME, PWD);
                stmt = connection.createStatement();
                 
                 
                //String sql = "select * from student where teacher='"+teachername+"'  " ;
                String sql = "select * from student where teacher like '%"+teachername+"%'  " ;
             
                // 执行SQL
                rs = stmt.executeQuery(sql);
                 
                // d.处理结果
                 
              
                     
                    while(rs.next()) {
                        String id=rs.getString("id");
                        System.out.println(id);
                        }
                     
                 
            catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("1");
            catch (SQLException e) {
                e.printStackTrace();
                System.out.println("3");
            catch(Exception e) {
                e.printStackTrace();
                System.out.println("2");
            }
            finally {
                try {
                     if(stmt!=null) stmt.close();// 对象.方法
                     if(connection!=null)connection.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
     
    }
  • 相关阅读:
    该虚拟机似乎正在使用中,请获取所有权
    分配销售区域
    未对销售组织 XXX 分销渠道 00 语言 ZH 定义
    Bash 遍历目录
    plsql 不需配置*.ora就可以访问数据库
    arch linux
    拓端数据tecdat|R语言有限混合模型(FMM,finite mixture model)及其EM算法聚类分析间歇泉喷发时间
    拓端数据tecdat|使用R语言进行多项式回归、非线性回归模型曲线拟合
    拓端数据tecdat|R语言使用ARIMAX预测失业率经济时间序列数据
    拓端数据tecdat|R语言用ARIMA模型,ARIMAX模型预测冰淇淋消费时间序列数据
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14916688.html
Copyright © 2011-2022 走看看