下面是一個從 mysql 數(shù)據(jù)庫獲取用戶信息的例子,可以參考一下:
網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了襄陽免費建站歡迎大家使用!
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.sql.Statement;
import?java.util.ArrayList;
import?java.util.List;
//?用戶類,存儲單個用戶信息
class?User?{
private?int?id;
private?String?name;
public?User(int?id,?String?name)?{
this.id?=?id;
this.name?=?name;
}
public?int?getId()?{
return?id;
}
public?void?setId(int?id)?{
this.id?=?id;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
@Override
public?String?toString()?{
return?"User?[id="?+?id?+?",?name="?+?name?+?"]";
}
}
public?class?Demo1?{
public?static?void?main(String[]?args)?throws?ClassNotFoundException,?SQLException?{
//?本例使用?mysql?數(shù)據(jù)庫,演示將數(shù)據(jù)庫?test?的?tb_users?表中的用戶信息
//?放到?List?中
//?加載數(shù)據(jù)驅(qū)動
Class.forName("com.mysql.jdbc.Driver");
//?數(shù)據(jù)庫連接字符串,?此例數(shù)據(jù)庫為?test
String?url?=?"jdbc:mysql://localhost:3306/test";
String?user?=?"root";????//?數(shù)據(jù)庫用戶名
String?password?=?"";????//?數(shù)據(jù)庫密碼
//?打開一個數(shù)據(jù)連接
Connection?conn?=?DriverManager.getConnection(url,?user,?password);
Statement?stmt?=?conn.createStatement();
//?獲取表?tb_users?所有用戶信息到結(jié)果集中
ResultSet?rs?=?stmt.executeQuery("SELECT?id,?name?FROM?tb_users");
//?定義一個存放用戶信息的?List
ListUser?users?=?new?ArrayList();
//?提取用戶信息,并將用戶信息放入?List
while?(rs.next())?{
//?獲取用戶ID
int?id?=?rs.getInt(1);
//?獲取用戶名
String?name?=?rs.getString(2);
users.add(new?User(id,?name));
}
rs.close();
stmt.close();
conn.close();
//?顯示用戶信息
for?(User?u?:?users)?{
System.out.println(u);
}
}
}
String?url?=?"jdbc:mysql://localhost:3306/bbsdb";?
String?username?=?"root";?
String?password?=?"pla";?
Class.forName("com.mysql.jdbc.Driver").newInstance();?
conn?=?DriverManager.getConnection(url,?username,?password);
區(qū)別在于url里面的localhost,這個是本機,要是訪問其他電腦,把這里localhost換成那個機器的IP地址,然后mysql的賬號密碼也要用那個電腦的mysql賬號密碼
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("數(shù)據(jù)庫連接成功");
} else {
System.out.println("數(shù)據(jù)庫連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 實例化 Statement 對象
Statement statement = getConnection().createStatement();
// 要執(zhí)行的 Mysql 數(shù)據(jù)庫操作語句(增、刪、改、查)
String sql = "";
// 展開結(jié)果集數(shù)據(jù)庫
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通過字段檢索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 輸出數(shù)據(jù)
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次關閉
resultSet.close();
statement.close();
getConnection().close();
}
}