1.注冊(cè)驅(qū)動(dòng) Class.forname("com.mysql.jdbc.Driver");//這是連接mysql數(shù)據(jù)庫(kù)的驅(qū)動(dòng)
2.獲取數(shù)據(jù)庫(kù)連接 java.sql.Connection conn=java.sql.DriverManager.getConnection();
3.獲取表達(dá)式 java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三個(gè)參數(shù)分別是數(shù)據(jù)庫(kù)連接的URL,用戶名,密碼
4.執(zhí)行SQL java.sql.ResultSet rs=stmt.executeQuery("select * from user");
5.顯示結(jié)果集里面的數(shù)據(jù) while(rs.next()){ System.out.println(rs.getInt(1)); System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); System.out.pringln(); }
//執(zhí)行插入語(yǔ)句 //stmt.executeUpdate("insert into user values(1,'中文','345')");
6.釋放資源 rs.close(); stmt.close(); conn.close();
|