//在jdbc中進行增刪查改mysql
//查看全部sql
public static void findAll() {
String url = "jdbc:mysql://localhost:3306/epet";//加載驅動器
String user = "root";
String password = "root";
String sql = "SELECT * FROM dog";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);//加載JDBC驅動器
statement = connection.createStatement();//與數據庫創建鏈接
resultSet = statement.executeQuery(sql);//發送SQL語句 ,而且返回結果
while(resultSet.next()){//處理返回結果
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getInt("health"));
System.out.println(resultSet.getInt("love"));
System.out.println(resultSet.getObject(5));
System.out.println("=================");
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try { //關閉資源
if (null != resultSet) {
resultSet.close();
}
if (null != statement) {
statement.close();
}
if (null != connection) {
connection.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
數據庫
//插入語句
public static void insert() {
String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "INSERT INTO dog(name,health,love,strain) VALUES ('aaa',90,100, 'bbb')";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}url
//更新數據庫
public static void update() {
String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "UPDATE dog SET name='haha' WHERE id=1";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}資源
//刪除數據庫元素
public static void delete() {
Connection connection = null;
Statement statement = null;
String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "DELETE FROM dog WHERE id =1";
String sql2 = "DELETE FROM dog WHERE id =2";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
statement.executeUpdate(sql2);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}get
}io