zoukankan      html  css  js  c++  java
  • java JDBC测试

    下载mysql-connector-java-5.1.31.jar添加到工程buildpath中

    package com.jdbc.test;
    
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import com.mysql.jdbc.Connection;
    
    
    public class JdbcTest1 {
        
        public static Connection getConnection(){
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");  //加载驱动
                conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","123456");  //连接数据库
            } catch (ClassNotFoundException | SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }
        
        public static void select(){
            Connection conn = getConnection();
            String sql = "select *from book1";
            try {
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(sql);  //执行SQL语句
                while(rs.next()){
                    System.out.print(rs.getString("use_name")+" ");
                    System.out.println(rs.getInt("use_age")+" ");
                }
                rs.close();
                st.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void insert(){
            Connection conn = getConnection();
            String sql = "insert into book1 values('lixiaoming',25)";
            try {
                Statement st = conn.createStatement();
                int count = st.executeUpdate(sql);
                System.out.println("已经向表中插入"+count+"条数据");
                st.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public static void delete(){
            Connection conn = getConnection();
            String sql = "delete from book1 where use_age = 25";
            try {
                Statement st = conn.createStatement();
                int count = st.executeUpdate(sql);
                System.out.println("从表中删除了"+count+"条数据");
                st.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        public static void update(){
            Connection conn = getConnection();
            String sql = "update  book1 set use_age=30 where use_name = 'zhangsan'";
            try {
                Statement st = conn.createStatement();
                int count = st.executeUpdate(sql);
                System.out.println("从表中更新了"+count+"条数据");
                st.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            select();
    //        insert();
    //        delete();
    //        update();
        }
    }
  • 相关阅读:
    hdu 1569 方格取数(2) 最大权独立集
    TCL脚本语言基础介绍
    Ubuntu10.04下Ckermit的使用
    LPC2468 Keil里设置RAM或Flash调试方法
    发布Android程式步骤
    Vim使用笔记(1): 安装
    在Android SDK里新建虚拟机出现:could not find virtual device named 'xxx'的解决办法
    git简单入门教程
    Jlink调试LPC2468
    Openrd 烧录uboot步骤
  • 原文地址:https://www.cnblogs.com/gongjian/p/6160978.html
Copyright © 2011-2022 走看看