jdbc连接数据库Mysql实例

说明:

  首先,需要下载MySQL数据库的驱动:mysql-connector-java-5.1.7-bin

  然后,准备数据库信息:MySQL用户名及密码,root/root

             jdbc连接的数据库:test

①在eclipse中通过jdbc连接MySQL数据库test:

public  class  Query{

  public static void main(String[] args){

     Connection conn=getConnection("root","root");

    //创建表格
      create(conn);
      //插入数据:
     insert(conn);
      //修改数据
      update(conn);
      //查询数据:
      query(conn);
       releaseConnection(conn);

  }

  //查询数据,定义query方法
   public static void query(Connection conn){
      String  Sql="select * from employee;";
  
      try {
         Statement stmt = conn.createStatement();
         ResultSet rs=stmt.executeQuery(Sql);
         rs.afterLast();
         while(rs.previous()){
          System.out.println("编号:"+rs.getString("id")+"姓名:"+rs.getString("dname")
             +rs.getString("address"));
       }
       if(rs!=null){
        rs.close();
       }
       if(stmt!=null){
        stmt.close();
       }
       if(conn!=null){
        conn.close();
       }
    } catch (SQLException e) {
       System.out.println("query:创建Statement失败");
       e.printStackTrace();
    } 
 }

 

   //修改数据,定义update方法
   public static void update(Connection conn){
      String sql="update employee set  address = 'Nanjing2' where id=1;";
      try {
         Statement stmt = conn.createStatement();
         int count=stmt.executeUpdate(sql);
  
         System.out.println("数据修改成功");
      } catch (SQLException e) {
         System.out.println("update:创建statement对象失败");
         e.printStackTrace();
      }
 }

 

  //插入数据,定义insert方法
   public static void insert(Connection conn){
      String sql="insert into employee values(1,'gaofeng','Nanjing');";
      try {
         Statement stmt=conn.createStatement();
         int count=stmt.executeUpdate(sql);
   
         System.out.println("插入数据成功");
      } catch (SQLException e) {
         System.out.println("insert:创建statement对象失败");
         e.printStackTrace();
      }
   }

 

  //创建表格employee,定义create方法
   public static void create(Connection conn){
      String sql="create table employee(id int(11),dname varchar(20),address varchar(40));";
      try {
         Statement stmt=conn.createStatement();
         int count=stmt.executeUpdate(sql);
   
         System.out.println("创建表格成功");
      } catch (SQLException e) {
         System.out.println("create:创建statement对象失败");
         e.printStackTrace();
      }
   }

 

  //释放数据库连接:数据库使用完成后为了节省资源,应及时断开连接

  public static void releaseConnection(Connection conn){

    try{

      if(conn!=null)

        conn.close();

    }catch(SQLException e){

        System.out.println("releaseConnection:释放数据库连接失败");

        e.printStackTrace();

    }

  }

 

  //数据库连接

  public static Connection getConnection(String user,String password){

    Connection conn=null;

    String driver="com.mysql.jdbc.Driver";

    String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8";

    try{

      //注册(加载)驱动程序

      Class.forName(driver);

      try{

        //获取数据库连接

        conn=DriverManager.getConnection(url,user,password);

      }catch(SQLException e){

        System.out.println("getConnection:连接数据库失败");  

        e.printStackTrace();

      }

    }catch(ClassNotFoundException e){

      System.out.println("getConnection:加载驱动失败");   

      e.pritStackTrace();

    }

  }

} 

执行结果:

①连接数据库结果

②创建表运行结果

③插入数据运行结果

④修改表运行结果

⑤执行查询 运行结果

 

转载于:https://www.cnblogs.com/gf36500/p/6764216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值