首页 > 代码库 > JDBC

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());                    }                    }}

 

JDBC