java输出MYSQL数据库里面的数据最简单的实例
import java.sql.*;
public class JDBCExample {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/gbooks";
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "select * from admin";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", webtitle : " + rs.getString("webtitle"));
System.out.print(", adminhttp : " + rs.getString("adminhttp"));
System.out.print(", adminname : " + rs.getString("adminname"));
System.out.print(", adminpass : " + rs.getString("adminpass"));
System.out.print(", adminmail : " + rs.getString("adminmail"));
System.out.print(", webadvice : " + rs.getString("adminmail"));
System.out.println(", webadvice : " + rs.getString("webadvice"));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
}
****************************************************************************************【来自我另一博文】
--------------------------------------------------------------------------------------------------------------------------------------------
作者:Honey_Badger —— 觉得这文章好,点一下左下角
出处:http://tk55.cnblogs.com/
posted on 2016-11-15 10:41 Honey_Badger 阅读(1921) 评论(0) 收藏 举报
浙公网安备 33010602011771号