[英]Connect MySQL to Java using Netbeans IDE
我想問一下如何在MySQL中連接Java程序,請幫幫我! 可以,請給我一個演示只是為了顯示數據庫上的記錄,謝謝!
127.0.0.1:3306
取決於數據庫的設置,請確保您在庫中添加了MySQL JDBC Driver,然后嘗試執行此代碼。
try{
Class.forName("com.mysql.jdbc.Driver"); // to set the driver
Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", "<Database username>", "<Database password>"); // connect to database
Statement st = connect.createStatement();
Resultset rs = st.executeQuery( "SELECT * FROM <tablename>" );
while( rs.next() ){
System.out.print( rs.getString(1) );
}
}
catch( Exception e ){
System.out.print( e );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.