3.4学习总结

完善了一下增删改查;

package dailysummer;

import java.sql.DriverManager;
import java.sql.Connection;

public class Main {

 


public void write(String title,String content,String keyword,String time) throws Exception {
String sql = "INSERT into daily VALUES('"+title+"','"+content+"','"+keyword+"','"+time+"')";


Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username = "root";
String Password = "123123";

Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();

stmt.executeUpdate(sql);
stmt.close();
}

public void delete(String title) throws Exception {
String sql = "delete from daily where title = '"+title+"'";

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username = "root";
String Password = "123123";

Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();

stmt.executeUpdate(sql);
stmt.close();
}

public void update(String time,String keyword1,String content1,String title1) throws Exception {
String sql = "update daily set keyword = '"+keyword1+"',title = '"+title1+"',content = '"+content1+"' where time = '"+time+"'";


Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username = "root";
String Password = "123123";

Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();

stmt.executeUpdate(sql);
stmt.close();
}



public String check(int m,String time) throws Exception {
String a = null;
String sql = "SELECT * FROM daily where time like '%"+time+"%'" ;


Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username= "root";
String Password= "123123";


Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();

java.sql.ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
a = rs.getString(m);

stmt.close();
rs.close();

return a;

}







}

posted @ 2023-03-04 21:01  听着DJ读童话  阅读(17)  评论(0)    收藏  举报