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();
                }
            }
        }
     
     
    }
  • 相关阅读:
    去过的论坛重新注册了
    for … in … 语句的使用
    删除整个目录
    使用Live Writer测试 1
    Google ProtocolBuffer.net简介与使用
    linq to sql 系列之 linq to sql性能优化技巧
    单元测试之道(使用NUnit)
    提高软件测试能力的19条建议
    System.Runtime.Caching;
    EneityFramework+DomainDataSource+Silverlight完成数据读取分页排序与修改
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14916688.html
Copyright © 2011-2022 走看看