java连接MySQL数据库操作步骤

package com.swift;
//这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection
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.List;

public class TestJDBC {

    public static void main(String[] args) {
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            //1、装载驱动
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //2、链接数据库,使用com.mysql.jdbc.Connection包会出错
            List<User> list=new ArrayList<User>();
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root");
            //3、创建连接语句
            st=conn.createStatement();
            //4、执行SQL语句获得结果集
            rs=st.executeQuery("select * from sw_user");
            //5、循环获得数据库字段生成对象
            while(rs.next()) {
                int id=rs.getInt("id");
                String username=rs.getString("username");
                String password=rs.getString("password");
                User user=new User(id,username,password);
                list.add(user);
            }
            //6、遍历对象列表
            for(User user:list) {
                System.out.println(user.toString());
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //7、关闭结果集
            try {
                if(rs!=null) {
                   rs.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //7、关闭连接语句
            try {
                if(st!=null) {
                   st.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //7、关闭数据库连接
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
}

 自己重写的,使用了BeanUtils 的反射技术  驱动管理器得到连接 连接得到sql语句 语句执行得到结果集

package com.swift.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.swift.bean.User;


public class JDBC_Demo {

    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String user="root";
        String password="root";
        String url="jdbc:mysql://127.0.0.1:3306/swift_db";
        Connection con=DriverManager.getConnection(url, user, password);
        PreparedStatement sql = con.prepareStatement("select * from user");
        ResultSet result = sql.executeQuery();
        List<User> list =new ArrayList<>();
        while(result.next()) {
            int uid=result.getInt("uid");
            String uname=result.getString("uname");
            String upassword=result.getString("upassword");
            Class clazz=Class.forName("com.swift.bean.User");
            User userObj=(User) clazz.newInstance();
            BeanUtils.setProperty(userObj,"uid",uid);
            BeanUtils.setProperty(userObj,"uname",uname);
            BeanUtils.setProperty(userObj,"upassword",upassword);
            list.add(userObj);
        }
        System.out.println(list);
    }

}

使用的Jar包如下

 

posted @ 2017-09-20 18:54  Advancing-Swift  阅读(661)  评论(0编辑  收藏  举报