java 连接 mysql TestJDBC {

来源:互联网 发布:高考物理满分知乎 编辑:程序博客网 时间:2024/05/21 14:45
import java.sql.*;   
  
public class TestJDBC {   
  
    public static void main(String[] args) {   
        ResultSet rs = null;   
        Statement stmt = null;   
        Connection conn = null;   
        try {   
            String URL = "jdbc:mysql://127.0.0.1:3306/multi";   
            String user = "root";   
            String password = "123";   
            Class.forName("com.mysql.jdbc.Driver");   
            conn = DriverManager.getConnection(URL, user, password);   
            stmt = conn.createStatement();   
            rs = stmt.executeQuery("SELECT Rbh,Rgly,Rlx,Rbz FROM RJBB");   
            while (rs.next()) {   
                System.out.print(rs.getString("Rbh"));   
                System.out.print(rs.getString("Rgly") + "  ");   
                System.out.print(rs.getString("Rlx") + "  ");   
                System.out.print(rs.getString("Rbz") + "  ");   
                System.out.print("\n");   
            }   
        } catch (ClassNotFoundException e) {   
            e.printStackTrace();   
        } catch (SQLException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                if (rs != null) {   
                    rs.close();   
                    rs = null;   
                }   
                if (stmt != null) {   
                    stmt.close();   
                    stmt = null;   
                }   
                if (conn != null) {   
                    conn.close();   
                    conn = null;   
                }   
            } catch (SQLException e) {   
                e.printStackTrace();   
            }   
        }   
    }   
  
}