java连接mysql表格_JDBC对数据库表格的操作

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public classdemo1 {/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub//查看

findAll();//增加

inset();//删除//delet();//更新//gengxin();

}//更新

public static voidgengxin(){

Connection connection=null;

Statement statement=null;

String url="jdbc:mysql://localhost:3306/epet";

String user="root";

String password="root";

String sql= "UPDATE master SET name='ljy',password='gggg',money=1000 WHERE id=118";//驱动

try{

Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}//获得连接对象

try{

connection=DriverManager.getConnection(url, user, password);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//获取Statement

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 voiddelet(){

Connection connection=null;

Statement statement=null;

String url="jdbc:mysql://localhost:3306/epet";

String user="root";

String password="root";

String sql= "DELETE FROM master WHERE id=117";//驱动

try{

Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}//获得连接对象

try{

connection=DriverManager.getConnection(url, user, password);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//获取Statement

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 voidfindAll(){

Connection connection=null;

Statement statement=null;

ResultSet resultSet= null;

String url="jdbc:mysql://localhost:3306/epet";

String user="root";

String password="root";

String sqlsString= "SELECT * FROM master";//驱动

try{

Class.forName("com.mysql.jdbc.Driver");

connection=DriverManager.getConnection(url, user, password);

statement=connection.createStatement();

resultSet=statement.executeQuery(sqlsString);while(resultSet.next()){

System.out.println(resultSet.getObject("id")+"\t");

System.out.println(resultSet.getObject("name")+"\t");

System.out.println(resultSet.getObject("password")+"\t");

System.out.println(resultSet.getObject("money")+"\t");

System.out.println("*******************************");

}

}catch (SQLException |ClassNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}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 voidinset(){

Connection connection=null;

Statement statement=null;

String url="jdbc:mysql://localhost:3306/epet";

String user="root";

String password="root";

String sqlString="INSERT INTO master(name,password,money) VALUES ('zhh','v5',10000000)";//驱动

try{

Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}//获得连接对象

try{

connection=DriverManager.getConnection(url, user, password);

System.out.println("连接成功");

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//获取Statement

try{

statement=connection.createStatement();

statement.executeUpdate(sqlString);

System.out.println("添加成功");

}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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值