jdbc学习

主要学习了:
jdbc的数据库连接,以及对mysql数据库的增、删、改、查。

package come.test.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class helloJdbc {

public static Connection GetConnection()
{
    Connection conn = null;
try
{
    Class.forName("com.mysql.jdbc.Driver");
    conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test_DB?characterEncoding=GBK","root","spring");
} catch (Exception e) 
{
      System.err.println(e.getMessage());
}
    return conn;
}

public static void SelectData()
{
    String str = "select * from tbl_user";
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    try {

        conn = GetConnection();
        st = (Statement) conn.createStatement();
        rs =st.executeQuery(str);

        while(rs.next())
        {
            System.out.print(rs.getInt("id")+",");
            System.out.print(rs.getString("name")+",");
            System.out.print(rs.getString("password")+",");
            System.out.print(rs.getString("email")+",");
            System.out.println();
        }

    } catch (Exception e) {
      System.err.println(e.getMessage());
    }finally {
        try{
            rs.close();
        }catch(Exception e){System.out.println(e.getMessage());}
        try{
            st.close();
        }catch(Exception e){System.out.println(e.getMessage());}
        try{
            conn.close();
        }catch(Exception e){System.out.println(e.getMessage());}
    }

}

public static void ADddData(){
    Connection conn =GetConnection();
    try {
      String str = "insert into tbl_User(name,password,email) values ('王五','123','ww@qq.com'),('赵六','123','zl@qq.com')";
      Statement st = (Statement) conn.createStatement();
      int count =st.executeUpdate(str);
      System.out.println("插入数据:"+count+"条");
      conn.close();

    } catch (Exception e) {
        System.err.println("插入数据失败!"+e.getMessage());
    }

}

public static void UpdateData(){
    Connection conn =GetConnection();
    try {
      String str = "update tbl_User set name='张三丰' where name='张三'";
      Statement st = (Statement) conn.createStatement();
      int count =st.executeUpdate(str);
      System.out.println("修改数据:"+count+"条");
      conn.close();

    } catch (Exception e) {
        System.err.println("修改数据失败!"+e.getMessage());
    }

}

public static void DeleteData(){
    Connection conn =GetConnection();
    try {
      String str = "delete from tbl_User where name ='赵六'";
      Statement st = (Statement) conn.createStatement();
      int count =st.executeUpdate(str);
      System.out.println("修改数据:"+count+"条");
      conn.close();

    } catch (Exception e) {
        System.err.println("修改数据失败!"+e.getMessage());
    }

}
public static void main(String[] args) {
    System.err.println("-----第一次查询数据");
    SelectData();
      System.err.println("执行插入数据");
      ADddData();
      System.err.println("-----插入后查询数据");
    SelectData();
    System.err.println("修改数据");
    UpdateData();
    System.err.println("-----修改后查询数据");
    SelectData();
     System.err.println("删除数据");
     DeleteData();
     System.err.println("----删除后查询数据");
    SelectData();

}

}
源代码地址:http://pan.baidu.com/s/1qWRRBZI

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值