mysql java例子_Java操作mysql数据库简单例子

public static final String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8"; //连接数据库的URL地址

public static String username = "root"; //数据库的用户名

public static String password = "";  //数据库的密码

public static Connection conn=null;//连接对象

public static Statement stmt=null;//语句

public static ResultSet rs = null;//结果集

//1.加载MySQL数据库驱动

static

{

try {

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

//2、建立数据库连接

conn = (Connection) DriverManager.getConnection(url,username,password);

if(conn != null)

{

System.out.println("数据库连接正常");

}

else

{

System.out.println("数据库连接失败");

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

//查询所有的学生资料

public static void query()

{

String sql = "select * from students;";

try {

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

while(rs.next())

{

System.out.println("学号:"+rs.getInt("sid")+",姓名:"+rs.getString("sname")+",年龄:"+rs.getInt("age")+",性别:"+rs.getString("gender"));

}

} catch (Exception e)

{

e.printStackTrace();

}

finally

{

destoryResource();

}

}

//添加学生方法

public static boolean add()

{

String sql = "insert into Students values (11,‘张三天‘,138,‘f‘,‘zhangsan@qq.com‘,‘广州阳江‘);";

try

{

stmt = conn.createStatement();

int result = stmt.executeUpdate(sql);

if(result > 0)

{

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

return true;

}

else

{

System.out.println("数据库添加失败");

return false;

}

}

catch(Exception ex)

{

ex.printStackTrace();

return false;

}

finally

{

destoryResource();

}

}

//释放资源的方法

public static void destoryResource()

{

try {

if(rs != null)

{

rs.close();

rs = null;

}

if (stmt != null)

{

stmt.close();

stmt = null;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

//释放最后资源

public static void destoryallResource()

{

try

{

if (conn != null)

{

conn.close();

conn = null;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

//删除指定学号的学生资料

public static boolean delete(int sid)

{

String sql = "delete from students where sid="+sid;

try

{

stmt = conn.createStatement();

int result = stmt.executeUpdate(sql);

if(result>0)

{

System.out.println("数据添删除成功");

return true;

}

else

{

System.out.println("数据添没有删除");

return false;

}

}

catch(Exception ex)

{

ex.printStackTrace();

return false;

}

finally

{

destoryResource();

}

}

//修改所有学生的年龄为20岁

public static boolean update(int age)

{

String sql = "update students set age="+age;

try

{

stmt = conn.createStatement();

int result = stmt.executeUpdate(sql);

if(result>0)

{

return true;

}

else

{

return false;

}

}

catch(Exception ex)

{

ex.printStackTrace();

return false;

}

finally

{

destoryResource();

}

}

public static void main(String[] args)

{

jdbc02.query();  //查询语句

if (jdbc02.add())

{

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

}

else

{

System.out.println("添加失败!");

}

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

jdbc02.query();

jdbc02.delete(11);

System.out.println("------删除学号为11的学生之后--------");

jdbc02.query();

jdbc02.update(20);

System.out.println("------修改所有学生年龄为20岁--------");

jdbc02.query();

jdbc02.destoryallResource();  //释放资源

}

}

本文出自 “知止内明” 博客,请务必保留此出处http://357712148.blog.51cto.com/6440370/1894631

Java操作mysql数据库简单例子

标签:java操作mysql例子

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:http://357712148.blog.51cto.com/6440370/1894631

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值