• 团队冲刺3 5.4


    开发环境:eclipse;

    语言:Java;

    数据库:mysql;

    第一步:首先是需要一个jar包(mysql-connector-java-xxxx.jar) 注:xxxx是版本,我用的是8.0.24。

    第二步:引入这个jar包,右键你的项目,构建路径,配置构建路径,库,在类路径里,添加外部jar(x)...选中jar 引入完成。

    第三步:连接

    package link;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Lei {
        public static String url = "jdbc:mysql://你云数据库的公网ip:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
        public static String user = "你的用户名";
        public static String password = "对应的密码";
        public static void main(String[] args) {
            Connection connect=null;
            Statement statement=null;
            ResultSet result=null;        
            try {
                connect = getConnection();
                statement=connect.createStatement();            
                String Query = "你的查询语句(select * from 表名 where 列名 = '?')";        
                result = statement.executeQuery(Query);            
                if(result.next()){
                    
                }else {
                        
                }    
                result.close();
                statement.close();
                close(connect);
            } catch (SQLException e) {
                 e.printStackTrace();
            }
    
        }
        //申请链接
        public static  Connection getConnection() {
            Connection con=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                con=DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            return con;
        }
        //关闭链接
        public static void close(Connection con) {
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }                 
        }
    }

    这是一个十分简单实例,简单到我感觉都不用写注释。

    useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B80

    这串参数该加的差不多都加了,如果还是报错的话直接去搜搜吧。

    前两个指定字符的编码、解码格式。第三个高版本的mysql需要指明是否ssl连接,根据自己的需求改。第四个设置时区,这个有时候很关键。

  • 相关阅读:
    java.io.IOException: HTTPS hostname wrong: should be 规格严格
    linux syslog 规格严格
    SVN,HG,GIT 命令说明 规格严格
    pclose : no child process 规格严格
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    Netbeans大战Eclipse 谁将走向祭坛?
    XP中的重要惯例和规则
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    简简单单删除所有.svn目录
    简简单单删除所有.svn目录
  • 原文地址:https://www.cnblogs.com/da48/p/14862166.html
Copyright © 2020-2023  润新知