1、准備要顯示的圖片,並創建一個最簡單的jsp頁面,運行起來。
Ⅱ java中怎麼獲取mysql資料庫的數據
用JDBC連接資料庫,然後用sql語句。要導入的驅動包。
import java.sql.*;
public class TestMySql {
static Connection con = null; // 聲明Connection對象
static Statement sql = null;
static ResultSet res = null;
public static void main(String[] args) {
TestMySql c = new TestMySql();
con = c.getConnection();
try {
sql = con.createStatement();
res = sql.executeQuery("select * from dept");
//sql語句,我資料庫里有張dept表
while (res.next()) {//輸出結果
System.out.print(res.getString(1) + "<——>");
System.out.print(res.getString(2) + "<——>");
System.out.print(res.getString(3) );
System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (res != null) {
res.close();
res =null;
}
if (sql != null) {
sql.close();
sql =null;
}
if (con != null) {
con.close();
con =null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
// 載入oracleJDBC驅動
System.out.println("資料庫驅動載入成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {// 通過訪問資料庫的URL獲取資料庫連接對象
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydata", "root", "qwer1234");
//mydata為mysql名字
System.out.println("資料庫連接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con; // 按方法要求返回一個Connection對象
}
}