• java与mysql连接


    package DBHelper;
     
    import java.sql.*;
     
    public class Demo {
        public static void main(String[] args) {
            String driver = "com.mysql.jdbc.Driver";
            String dbName = "test";
            String passwrod = "root";
            String userName = "root";
            String url = "jdbc:mysql://localhost:3306/" + dbName;
            String sql = "select * from test";
     
            try {
                Class.forName(driver);
                Connection conn = DriverManager.getConnection(url, userName,
                        passwrod);
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println("id : " + rs.getInt(1) + " name : "
                            + rs.getString(2) );
                }
     
                // 关闭记录集
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
     
                // 关闭声明
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
     
                // 关闭链接对象
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
    }
  • 相关阅读:
    PHP函数
    git (1)
    JavaScript(4)
    javascript-DOM(3)
    JavaScript-DOM(2)
    [转]分布式架构知识体系
    Mysql中查看每个IP的连接数
    Git常用命令备忘录
    windows下用vscode写C++
    sudo cd为什么不能够执行?
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/4909192.html
Copyright © 2020-2023  润新知