• MySQL JBDC 连接测试


    package cn.xh.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Demo01 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Statement st=null;
            Connection conn=null;
                
            try {
                //1.加载MYSQL 数据库驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2.创建连接
                //定义链接(注意设置编码UTF-8)
                 //String url ="jdbc:mysql://192.168.47.10:3306/students?user=root&password=123456.&useUnicode=true&characterEncoding=utf8" ;
                 String user= "root";  //定义用户
                 String password="123456";  //定义密码  --studentss  是数据库
                 String url2="jdbc:mysql://192.168.47.10:3306/students?useUncode=trun&characterEncoding=UTF-8";  //定义地址
                 System.out.println("开始连接当中......");
                 conn = DriverManager.getConnection(url2,user,password);   //连接参数
                 System.out.println("连接成功!");
                                  
                //3.创建 statement,执行操作
                 
                 String sql = "INSERT INTO tb_stu(id,NAME,sex,birthday,age) VALUES(15,'大王','男','2013-6-3','8')";
                 st =    conn.createStatement(); 
                 int result =  st.executeUpdate(sql);
            
                 System.out.println(result);
                    
                
                
                
            } catch (ClassNotFoundException e) {
                    e.printStackTrace();
            }catch (SQLException e) {
                    e.printStackTrace();
            }
            finally {
                if(st !=null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                }
                if(conn !=null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }    
            }
            
        }
        }
    明天的你会感谢今天拼命奋斗的自己
  • 相关阅读:
    bcdedit /copy {current} /d "xxx" 报错,提示找不到系统文件
    Moving docker images location to different partition
    docker 使用save和load命令来转移image
    docker image rm ubuntu 失败
    yum国内镜像配置
    VMware下安装CentOS7 无法通过桥接模式进行联网
    docker大概理解
    windows cmd 切换磁盘
    使用Git向GitHub上上传代码
    抛砖引玉——进程和线程的理解方式
  • 原文地址:https://www.cnblogs.com/qzqdy/p/15376424.html
Copyright © 2020-2023  润新知