package com.wang;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class Test1 {
//创建对象读取资源文件
private static final Properties p=new Properties();
//定义
private static String user,pwd,url,driver;
static {
try {
//从类路径下获取读取文件到内存中
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("login.properties"));
user=p.getProperty("username");
pwd=p.getProperty("password");
url=p.getProperty("url");
driver=p.getProperty("driver");
// System.out.println(user);
// System.out.println(pwd);
// System.out.println(url);
// System.out.println(driver);
Class.forName(driver);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private static Connection connection;
private static Statement statement;
private static ResultSet resultSet;
public static Connection getConnection() throws SQLException {
//获取驱动
connection= DriverManager.getConnection(url,user,pwd);
return connection;
}
public static void main(String[] args) throws SQLException {
//执行对象
statement=getConnection().createStatement();
//查询条件定义
String user="三国演义" ,user1="罗贯中";
//sql语句
String sql="select * from tb_book where title='"+user+"'and author='"+user1+"'";
resultSet=statement.executeQuery(sql);
if(resultSet.next()){
System.out.println("成功");
}else {
System.out.println("失败");
}
}
}
package com.wang;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql:///book_db","root","root");
//执行对象
Statement statement=connection.createStatement();
//执行的sql语句
ResultSet re=statement.executeQuery("select * from tb_book");
//判断是否存在
while(re.next()){
System.out.println(re.getString("title"));
System.out.println(re.getString("author"));
System.out.println(re.getInt("price"));
}
}
}
package com.wang;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql:///book_db","root","root");
//执行对象
Statement statement=connection.createStatement();
//执行的sql语句
String sql="insert into tb_book(title,author,price)values('三','四',2)";
//返回影响行数
String delete="delete from tb_book where title='三'";
int m=statement.executeUpdate(delete);
if(m==1){
System.out.println("新增");
}else {
System.out.println("失败");
}
}
}