数据库设计
添加操作
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AddBooks {
public static void main(String[] args) throws SQLException {
//1,获得数据源DataSource(设置Mysql的服务器地址)
MysqlDataSource dataSource=new MysqlDataSource();
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&&useSSL=true");
dataSource.setUser("root");
dataSource.setPassword("123456");
//2.获得连接
Connection connection= (Connection) dataSource.getConnection();
//3.得到执行器
String insertSQL="insert into books(name,author) values(?,?)";
PreparedStatement statement=connection.prepareStatement(insertSQL);
statement.setString(1,"水浒传");
statement.setString(2,"施耐庵");
//4.执行SQL
int result=statement.executeUpdate();
System.out.println("受影响的行数:"+result);
//5.关闭资源
statement.close();
connection.close();
}
}
修改操作
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UpBooks {
public static void main(String[] args) throws SQLException {
MysqlDataSource dataSource=new MysqlDataSource();
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&useSSL=true");
dataSource.setUser("root");
dataSource.setPassword("123456");
Connection connection=(Connection) dataSource.getConnection();
String updateSql="update books set name=? where id=?";
PreparedStatement statement=connection.prepareStatement(updateSql);
statement.setString(1,"西游记2");
statement.setInt(2,1);
int result=statement.executeUpdate();
System.out.println("受影响的行数:"+result);
statement.close();
connection.close();
}
}
删除操作
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//删除操作
public class DelBooks {
public static void main(String[] args) throws SQLException {
MysqlDataSource dataSource=new MysqlDataSource();
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEnding=utf8&useSSL=true");
dataSource.setUser("root");
dataSource.setPassword("123456");
Connection connection=(Connection) dataSource.getConnection();
String delSql="delete from books where id=?";
PreparedStatement statement=connection.prepareStatement(delSql);
statement.setInt(1,1);
int result=statement.executeUpdate();
System.out.println("受影响的行数:"+result);
statement.close();
connection.close();
}
}
查询操作
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//查询操作
public class SelBooks {
public static void main(String[] args) throws SQLException {
//1.获得数据源
MysqlDataSource dataSource=new MysqlDataSource();
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&useSSL=true");
dataSource.setUser("root");
dataSource.setPassword("123456");
//2.获得连接
Connection connection=(Connection)dataSource.getConnection();
//3.获得执行器
String sql="select * from books where id<?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setInt(1,5);
//4.执行SQL,打印查询结果
ResultSet resultSet=statement.executeQuery();
while(resultSet.next()){
Books books=new Books();
books.setId(resultSet.getInt("id"));
books.getName(resultSet.getString("name"));
books.setAuthor(resultSet.getString("author"));
System.out.println(books);
}
//关闭资源
resultSet.close();
statement.close();
connection.close();
}
}