java 连接数据库(MYSQL)的经典代码

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();
			}
		}
	}

}

 

你可能感兴趣的:(java,sql,mysql,jdbc)