package db; import java.sql.*; import javax.sql.*; public class ConnectMySQL{ String dbUrl, username,password; Connection con = null; public ConnectMySQL(String url, String dbName, String usr, String pwd){ String dbtime; dbUrl = "jdbc:mysql://"+url+"/"+dbName;//your.database.domain/yourDBname"; String dbClass = "com.mysql.jdbc.Driver"; String query = "Select * FROM users"; username = usr; password = pwd; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection (dbUrl,usr,pwd); } //end try catch(ClassNotFoundException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } } public void closeDB(){ try { if(con != null) con.close(); } //end try catch(SQLException e) { e.printStackTrace(); } }// end closeDB public ResultSet select(String sqlCode){ ResultSet rs = null; try{ if(con != null){ Statement st = con.createStatement(); rs = st.executeQuery(sqlCode); } }catch(SQLException e) { e.printStackTrace(); } return rs; } public boolean insert (String command)throws SQLException{ if(con != null){ Statement st = con.createStatement(); st.executeUpdate(command); return true; }else return false; } public boolean update (String command)throws SQLException{ if(con != null){ Statement st = con.createStatement(); st.executeUpdate(command); return true; }else return false; } public boolean delete (String command)throws SQLException{ if(con != null){ Statement st = con.createStatement(); st.executeUpdate(command); return true; }else return false; } public static void main(String args[]){ String dbtime; String dbUrl = "jdbc:mysql://your.database.domain/yourDBname"; String dbClass = "com.mysql.jdbc.Driver"; String query = "Select * FROM users"; try { //################################## Start distinct ################################################# //// คำสัง connect ConnectMySQL mysql = new ConnectMySQL("localhost","pnotebook","root","1111"); String cmd = "select weight_cpu,cputype_bit,cpu_speed from dcpu"; //คำสั่ง SQL ResultSet dcpu = mysql.select(cmd); while(dcpu.next()){ // ส่วนของการแสดง String weight_cpu=dcpu.getString("weight_cpu"); String cputype_bit=dcpu.getString("cputype_bit"); String cpu_speed=dcpu.getString("cpu_speed"); System.out.println(weight_cpu+" "+cputype_bit+" "+cpu_speed); } System.out.println("///////////////"); String cmd2 = "select * from dram"; //คำสั่ง SQL ResultSet dram = mysql.select(cmd2); while(dram.next()){ // ส่วนของการแสดง String weight_ram=dram.getString("weight_ram"); String ramtype_bit=dram.getString("ramtype_bit"); String ram_speed=dram.getString("ram_speed"); System.out.println(weight_ram+" "+ramtype_bit+" "+ram_speed); }
System.out.println("///////////////"); String cmd3 = "select * from dvga"; //คำสั่ง SQL ResultSet dvga = mysql.select(cmd3); while(dvga.next()){ // ส่วนของการแสดง String weight_vga=dvga.getString("weight_vga"); String vgatype_bit=dvga.getString("vgatype_bit"); String vga_speed=dvga.getString("vga_speed"); System.out.println(weight_vga+" "+vgatype_bit+" "+vga_speed); } System.out.println("///////////////"); String cmd4 = "select * from dweight"; //คำสั่ง SQL ResultSet dweight = mysql.select(cmd4); while(dweight.next()){ // ส่วนของการแสดง String weight_product=dweight.getString("weight_product"); System.out.println(weight_product); } //################################## End distinct ################################################# //################################## Start number distinct ################################################# System.out.println("///////////////"); String cmd5 = "select * from cdcpu"; //คำสั่ง SQL ResultSet ccpu = mysql.select(cmd5); while(ccpu.next()){ // ส่วนของการแสดง String cdcpu=ccpu.getString("ccpu"); System.out.println(cdcpu); } String cmd6 = "select * from cdram"; //คำสั่ง SQL ResultSet cram = mysql.select(cmd6); while(cram.next()){ // ส่วนของการแสดง String cdram=cram.getString("cram"); System.out.println(cdram); } String cmd7 = "select * from cdvga"; //คำสั่ง SQL ResultSet cvga = mysql.select(cmd7); while(cvga.next()){ // ส่วนของการแสดง String cdvga=cvga.getString("cvga"); System.out.println(cdvga); } String cmd8 = "select * from cdweight"; //คำสั่ง SQL ResultSet cweight = mysql.select(cmd8); while(cweight.next()){ // ส่วนของการแสดง String cdweight=cweight.getString("cweight"); } //################################## End number distinct ################################################# String cmd9 = "select * from productbit"; //คำสั่ง SQL ResultSet product = mysql.select(cmd9); while(product.next()){ // ส่วนของการแสดง String productid=product.getString("productid"); String cpu_bit=product.getString("cpu_bit"); String cputype_bit=product.getString("cputype_bit"); String cpu_speed=product.getString("cpu_speed"); String ram_bit=product.getString("ram_bit"); String ramtype_bit=product.getString("ramtype_bit"); String ram_speed=product.getString("ram_speed"); String vga_bit=product.getString("vga_bit"); String vgatype_bit=product.getString("vgatype_bit"); String vga_speed=product.getString("vga_speed"); String weight_bit=product.getString("weight_bit"); System.out.println(productid+" "+cpu_bit+" "+cputype_bit+" " +cpu_speed+" "+ram_bit+" "+ramtype_bit+" "+ram_speed+" " +vga_bit+" "+vgatype_bit+" "+vga_speed+" "+weight_bit); } mysql.closeDB(); // ปิดฐานข้อมูล }catch(Exception e) { e.printStackTrace(); } } //end main } //end class
StrProductID=product.getString("productid");
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง