首先,您必須創建與數據庫的連接,您可以使用JDBC進行連接。在構造函數中創建
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
public Dbconnection()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","root");
}
catch(Exception e)
{
System.out.println("Error in connection"+e);
}
}
連接,
方法從表中
public void getData()
{
try{
ps=con.prepareStatement("select * from tbl_name");
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1)); //here you can get data, the '1' indicates column number based on your query
}
}
catch(Exception e)
{
System.out.println("Error in getData"+e);
}
}
得到所有行while循環,你可以從rs
所有數據使用rs.getString(1)
注意:改變列號爲不同的列。
爲了獲得更多的想法,請參考鏈接:
http://www.tutorialspoint.com/jdbc/index.htm
http://www.w3schools.com/sql/
什麼編程語言? – Farrokh 2014-08-30 10:24:22
java dude,sorry – reza 2014-08-30 10:24:53
試試這篇文章:http://www.vogella.com/tutorials/MySQLJava/article.html – Farrokh 2014-08-30 10:27:16