jdbc(Java數據庫連接)


  jdbc 三件事
1、連接數據庫:(driver驅動, url username ,password)
2、發出 sql 指令:比如:查詢某個表格的所有數據  sql ="select * from table"
3、接受數據庫的反饋結果(ResultSet)
 
 jdbc 使 用:
 1、ip:本地ip:127.0.0.1(localhost
 2、sid  mysql:mysql
 3、端口號port: mysql:3306
 4、username:root(自己設置的mysql入口用戶名)
 5、password:admin(自己設置的mysql入口密碼)
 6、驅動名稱:mysql:com.mysql.jdbc.Driver  全限定名(包名+類名)(packagename + classname
 7、url連接地址:mysql:jdbc:mysql://localhost:3306/college  (mysql:sid  localhost:ip  3306:port  college:數據庫名稱)
 
jdbc連接mysql
 1、加載注冊一個驅動  Class.forName
 2、創建連接對象:Connection conn = DriverManager.getConnection();
 3、Statement statement = conn.createStatement();
 4、stament.executeQuery(sql);   stament.executeUpdate(sql);
 5、接收結果ResultSet   int
 
jdbc連接數據庫的代碼:
 */
	private static String driver = "";
	private static String url = "";
	private static String username = "";
	private static String password = "";
	private static Properties properties = new Properties();

	public static void main(String[] args) throws IOException {
//		獲取類加載器
		ClassLoader classLoader = JDBCTest.class.getClassLoader();
//		獲取輸入流  (properties文件)
		InputStream ips = classLoader.getResourceAsStream("jdbc.properties");
//		將輸入流  加載到 properties
		properties.load(ips);
		driver = properties.getProperty("jdbc.driver");
		url = properties.getProperty("jdbc.url");
		username = properties.getProperty("jdbc.username");
		password = properties.getProperty("jdbc.password");
		Connection connection = null;
//		Statement statement = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			Class.forName(driver);
			//創建連接對象
			connection = DriverManager.getConnection(url, username, password);
			//發送SQL語句
			String sql = "select * from t_user where username = ? and password = ?";
			//對信息語句進行預處理
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "admin");
			preparedStatement.setString(2, "123456");
//			statement = connection.createStatement();
			// 查詢
			rs = preparedStatement.executeQuery();
			// 增加 刪除 修改
			// statement.executeUpdate(sql)
			while (rs.next()) {
				System.out.println(rs.getInt("id") + "," + rs.getString("username") + "," + rs.getString("password"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//從下往上依次關閉
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
 
 
 
別忘了對應的jar包:
 
同時,要右擊選擇build path:
 
以及對應的properties文件:
至此,運行過后系統沒有報錯視為連接成功。
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM