首页 > 数据库技术 > 详细

jdbc连接mysql数据库

时间:2014-03-11 16:07:23      阅读:455      评论:0      收藏:0      [点我收藏+]

    //连接mysql数据库的步骤
    //1. 加载驱动
    //2.用drivermanager获得数据库连接
    //3.实例化QueryRunner
    //4.利用qr.update(),实现增删改
    //5.利用qr.query()得到结果集

bubuko.com,布布扣
 1 public class UtilMysql {
 2     //连接mysql数据库的步骤
 3     //1. 加载驱动
 4     //2.用drivermanager获得数据库连接
 5     //3.实例化QueryRunner
 6     //4.利用qr.update(),实现增删改
 7     //5.利用qr.query()得到结果集
 8     private static String driverClassName="com.mysql.jdbc.Driver";
 9     private static String userName="root";
10     private static String pwd="";
11     private static String url="jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=utf8";
12 
13     //加载驱动,获取数据库连接
14     public static Connection getConnection()
15     {
16         Connection conn=null;
17         try {
18             Class.forName(driverClassName);
19             conn= DriverManager.getConnection(url,userName,pwd);
20         } catch (ClassNotFoundException e) {
21             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
22         } catch (SQLException e) {
23             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
24         }
25        return conn;
26     }
27 
28 
29 }
bubuko.com,布布扣

2.实例类

bubuko.com,布布扣
public class PeopleBean {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
bubuko.com,布布扣

3.数据库的增删改查

bubuko.com,布布扣
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: kinkoo
 * Date: 14-3-11
 * Time: 上午11:08
 * To change this template use File | Settings | File Templates.
 */
public class MysqlDemo {

    //插入语句
    public static void insert_sql() throws SQLException {
        //获得数据库连接
        Connection con=UtilMysql.getConnection();
        //获取QueryRunner对象
        QueryRunner qrunner=new QueryRunner();
        //执行sql语句
        int n=qrunner.update(con,"insert into people(pname,age) values(‘bb‘,13)");
        System.out.println("成功插入"+n+"条数据");
        //关闭数据库
        DbUtils.closeQuietly(con);
    }
    //删除
    public static void delete_sql() throws SQLException {
        Connection con=UtilMysql.getConnection();
        QueryRunner qrunner=new QueryRunner();
        int n=qrunner.update(con,"delete from people where id=7");
        System.out.println("成功删除"+n+"条数据");
        DbUtils.closeQuietly(con);
    }
    //更新
    public static void update_sql() throws SQLException {
        Connection con=UtilMysql.getConnection();
        QueryRunner qrunner=new QueryRunner();
        int n=qrunner.update(con,"update people set pname=‘pp‘ where id=4");
        System.out.println("成功更新"+n+"条数据");
        DbUtils.closeQuietly(con);
    }
    //查询
    public static void select_sql() throws SQLException {
        Connection con=UtilMysql.getConnection();
        QueryRunner qrunner=new QueryRunner();
        List<Map> list=(List)qrunner.query(con,"select * from people",new MapListHandler());
        //输出结果集
        for(Map m:list)
        {
            System.out.println(m);
        }
        DbUtils.closeQuietly(con);
    }
    public static void main(String args[]) throws SQLException {
        insert_sql();
        delete_sql();;
        update_sql();
        select_sql();
    }
}
bubuko.com,布布扣

jdbc连接mysql数据库,布布扣,bubuko.com

jdbc连接mysql数据库

原文:http://www.cnblogs.com/Likyn/p/3593731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!