package Test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static Connection connection=null;
public static Statement statement=null;
public static void main(String[] args) {
//Insert();
//Delete(1);
Update(3);
// Select();
}
public static void Select(){
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
//写SQL语句
String sql = "select * from student";
//获取执行对象
statement = connection.createStatement();
//执行SQL语句
ResultSet rows= statement.executeQuery(sql);
System.out.println("id"+"\t"+"姓名");
//处理结果
while(rows.next()){
int id = rows.getInt("id");
String name = rows.getString("name");
System.out.println(id+"\t"+name);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("驱动注册异常");
}catch(SQLException e){
System.out.println("数据库连接异常");
}finally {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public static void Update(int id){
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
//写SQL语句
String sql = "update student set name='hehhehe' where id="+id;
//获取执行对象
statement = connection.createStatement();
//执行SQL语句
int row= statement.executeUpdate(sql);
//处理结果
if(row > 0){
System.out.println("更改成功");
}else {
System.out.println("更改失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("驱动注册异常");
}catch(SQLException e){
System.out.println("数据库连接异常");
}finally {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public static void Delete(int id){
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
//写SQL语句
String sql = "delete from student where id="+ id;
//获取执行对象
statement = connection.createStatement();
//执行SQL语句
int row= statement.executeUpdate(sql);
//处理结果
if(row > 0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("驱动注册异常");
}catch(SQLException e){
System.out.println("数据库连接异常");
}finally {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public static void Insert(){
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
//写SQL语句
String sql = "insert into student values (6,'hy',45,'女','100')";
//获取执行对象
statement = connection.createStatement();
//执行SQL语句
int row= statement.executeUpdate(sql);
//处理结果
if(row > 0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("驱动注册异常");
}catch(SQLException e){
System.out.println("数据库连接异常");
}finally {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}