java数据库原始连接

java数据库原始连接几种样式

方法一(今天自己闲下来时候写的)

public class Test {
    static String dirver = "com.mysql.jdbc.Driver";
    static String user = "root";
    static String password = "root";
    static String url = "jdbc:mysql://localhost:3306/ssh??useSSL=true";

    static Connection con=null;
    static Statement sta=null;
    static ResultSet res=null;
    public static void getData() throws ClassNotFoundException, SQLException {
           Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection(url, user, password);
         sta = con.createStatement();
        String sql = "select * from produce";
         res = sta.executeQuery(sql);
        while (res.next()) {

            System.out.println(res.getObject("name").toString());
            System.out.println(res.getObject("id").toString());
        }
    }
    public static void getclose(Connection con,Statement sta,ResultSet res) throws SQLException {
        if (res!=null)
        {
            res.close();
        };
        if (sta!=null)
        {
            sta.close();
        }
        if (con!=null)
        {
            con.close();
        }
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Test test = new Test();
        test.getData();
         test.getclose(con, sta, res);
    }

}

## 方法二:

public class Util {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL ="jdbc:mysql://localhost:3306/ssh?useSSL=true";

    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL

    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接

            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询

            stmt = conn.createStatement();
            String sql;
            sql = "select  * from produce";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);

                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

方法三:

public class Test03 {
    //提供获取连接的方法private static Connection conn=null;
    private static Connection conn=null;
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/ssh?useSSL=true";
            String username="root";
            String password="root";
            conn=DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e+"数据库连接失败");
        }

    }
    //获取数据库连接对象
    public static Connection getConnection(){
        return conn;
    }
    //关闭数据库的方法
    public static void close(ResultSet rs,Statement sta,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(sta!=null){
            try {
                sta.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement sta,Connection conn){
        if(sta!=null){
            try {
                sta.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws SQLException {
        Connection conn=Test03.getConnection();
        String sql="select  * from produce";
        PreparedStatement pst=conn.prepareStatement(sql);

        ResultSet resultSet=pst.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("name"));
        }

        Test03.close(pst, conn);
    }
}

框架用的太多了,感觉都不会写了,但是这个还是有时候会遇到,记录下来,个人喜欢第三种,网上的,以后想按这种方法写数据库连接了

注意点

1.jdbc:mysql://localhost:3306/ssh?useSSL=true,这写的数据库名,安全连接最好不要省略;

2.sql语句正确,可以先执行一下能不能查出来;

3.记得关闭数据库

最近一直在学js,js和java确实没差多少,学完js再来jquery,vue,同时还得做ssm项目,springboot,微服务。。。。。感觉后端太累了,前后端兼顾啊,最近还要复习考试,诶,加油吧。。。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值