zoukankan      html  css  js  c++  java
  • JDBC

    jdbc+mysql:

    package jcbc;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    import org.junit.Test;
    
    
    
    public class Jdbc {
        @Test
        public void jdbcConnection() throws IOException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException{
    //        步骤:1.注册驱动
    //            2.获取连接
    //            3.获取Statement对象
    /*配置文件jdbc.properties里面的内容    driver=com.mysql.jdbc.Driver
                                    url=jdbc:mysql://localhost:3306/mydb1?useSSL=true
                        
                                    user=root
                                    password=123456    */
            
            
            //类加载器加载配置文件
            Properties pro = new Properties();
            ClassLoader clazz = Jdbc.class.getClassLoader();
            //配置文件jdbc.properties在src目录下
            InputStream is = clazz.getResourceAsStream("jdbc.properties");
            pro.load(is);
            //检查配置文件是否加载成功
            System.out.println(pro.getProperty("driver"));
    //        注册数据库驱动
            Class.forName(pro.getProperty("driver")).newInstance();
            
    //        获取连接,导包的时候是导入import java.sql.Connection;
        
            Connection con = DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("user"), pro.getProperty("password"));
    //获取Statement对象的实例
            Statement statement = con.createStatement();
            //通过statement操作数据库
            ResultSet resultset = statement.executeQuery("Select * from customer");
            while(resultset.next()){
                System.out.println(resultset.getObject("name"));
            }
        
            
            
            
            
        }
    
    }

    java中的数据类型与mysql中的数据类型的对应关系

    将数据库的数据存入到List集合中

    package jcbc;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Properties;
    
    import org.junit.Test;
    
    import com.wxhledu.cn.domain.Customer;
    
    
    
    public class Jdbc {
        @Test
        public void jdbcConnection() throws IOException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException{
    //        步骤:1.注册驱动
    //            2.获取连接
    //            3.获取Statement对象
    /*配置文件jdbc.properties里面的内容    driver=com.mysql.jdbc.Driver
                                    url=jdbc:mysql://localhost:3306/mydb1?useSSL=true
                        
                                    user=root
                                    password=123456    */
            
            
            //类加载器加载配置文件
            Properties pro = new Properties();
            ClassLoader clazz = Jdbc.class.getClassLoader();
            //配置文件jdbc.properties在src目录下
            InputStream is = clazz.getResourceAsStream("jdbc.properties");
            pro.load(is);
            //检查配置文件是否加载成功
            System.out.println(pro.getProperty("driver"));
    //        注册数据库驱动
            Class.forName(pro.getProperty("driver")).newInstance();
            
    //        获取连接,导包的时候是导入import java.sql.Connection;
        
            Connection con = DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("user"), pro.getProperty("password"));
    //获取Statement对象的实例
            Statement statement = con.createStatement();
            //通过statement操作数据库
            ResultSet resultset = statement.executeQuery("Select * from customer");
            List<Customer> customers = new ArrayList<Customer>();
            while(resultset.next()){
                Customer customer = new Customer();//建立一个javabean封装Customer
                
                //获取数据库里面的数据,并且封装到customer中
                customer.setGender(resultset.getString("gender"));
                customer.setBirthday(resultset.getDate("birthday"));
                customer.setCellphone(resultset.getString("cellphone"));
                customer.setDescription(resultset.getString("description"));
                customer.setName(resultset.getString("name"));
                customer.setPreference(resultset.getString("preference"));
                customer.setType(resultset.getString("type"));
                
                //将封装好的对象存入到List集合中
                customers.add(customer);
            }
            Iterator it = customers.iterator();
            /*for(Customer c : customers){
                System.out.println(c);
            }
            */
            while(it.hasNext()){
                System.out.println(it.next().toString());
                
            }
            
            
        }
    
    }

     JNDI:

        步骤:1、将mysql-connector-java-5.1.39-bin的jar包拷到tomcat的lib目录下

           2、在

  • 相关阅读:
    (转)编写高质量高效率的SharePoint应用程序
    转:我眼中的Visual Studio 2010架构工具
    Windows 7 x64/Windows 2008 : The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine.
    转:构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)
    HTML5 到底是什么?
    使用eval()解析JSON格式字符串应注意的问题
    使用HTML5进行地理位置定位。误差在+500m
    LAST DAY
    javacript获取obj的长度
    通过 JSON 字符串来创建对象
  • 原文地址:https://www.cnblogs.com/huxuebing/p/5853121.html
Copyright © 2011-2022 走看看