zoukankan      html  css  js  c++  java
  • jdbc连接,操作

    package jdbca;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import com.mysql.jdbc.Driver;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import com.mysql.jdbc.Driver;

    public class DemoPreparedstement {
    public static void testselect(int cno) throws Exception {
    DriverManager.registerDriver(new Driver());
    // Class.forName("com.mysql.jdbc.Driver");
    // 2.获取连接对象
    String url = "jdbc:mysql://localhost:3306/a";
    Connection conn = DriverManager.getConnection(url, "root", "cjx990725");
    // 3.获取载体
    PreparedStatement pstmt = conn.prepareStatement("select * from coure where cno=?");
    // 给?设置值,从左到右赋值,序号从1开始,
    pstmt.setInt(1, cno);
    // 执行
    ResultSet rs = pstmt.executeQuery();//执行操作
    if (rs.next()) {
    int id = rs.getInt(1);
    String name = rs.getString(2);
    int cpno = rs.getInt(3);
    int ss = rs.getInt(4);
    System.out.println(id + " " + name + " " + cpno + " " + ss);
    }

    }

    public static void update(int cno,String cname) throws SQLException{
    DriverManager.registerDriver(new Driver());
    String url="jdbc:mysql://localhost:3306/a";
    Connection conn=DriverManager.getConnection(url, "root", "cjx990725");
    System.out.println(conn);
    PreparedStatement pstmt = conn.prepareStatement("update coure set cname=? where cno=?");
    pstmt.setString(1, cname);
    pstmt.setInt(2, cno);
    int n= pstmt.executeUpdate();//返回int类型
    System.out.println(n);
    pstmt.close();
    conn.close();
    }
    public static void delete(int cno) throws SQLException{
    DriverManager.registerDriver(new Driver());
    String url="jdbc:mysql://localhost:3306/a";
    Connection conn=DriverManager.getConnection(url, "root", "cjx990725");
    System.out.println(conn);
    PreparedStatement pstmt = conn.prepareStatement("delete from coure where cno=?");
    pstmt.setInt(1, cno);
    int n= pstmt.executeUpdate();
    System.out.println(n);
    pstmt.close();
    conn.close();
    }
    public static void insert(String cname,int cpno,int ccredit,int cno) throws SQLException{
    DriverManager.registerDriver(new Driver());
    String url="jdbc:mysql://localhost:3306/a";
    Connection conn=DriverManager.getConnection(url, "root", "cjx990725");
    PreparedStatement pstmt = conn.prepareStatement("insert into coure(cno,cname,cpno,ccredit) values(?,?,?,?)");
    pstmt.setString(2, cname);
    pstmt.setInt(3, cpno);
    pstmt.setInt(4, ccredit);
    pstmt.setInt(1, cno);
    int n= pstmt.executeUpdate();
    System.out.println(n);
    pstmt.close();
    conn.close();
    }
    public static void main(String[] args) throws Exception {
    //testselect(5);
    //insert("李四",5,6,9);
    update(9,"张三");
    //delete(9);
    }
    }

  • 相关阅读:
    我的第一篇博客缓存显示图片
    CSLA.Net 学习 WCF服务端与客户端配置
    CSLA.Net 学习 刚接触
    [转] DevExpress 第三方控件汉化的全部代码和使用方法
    java版飞信协议实现
    [转]C#反射技术之一读取和设置类的属性
    NHibernate帮助类
    Oracle 11g 精简客户端打包 201206更新
    Mygeneration模板(NHibernate)生成,根据kdup的修改而来
    [转]TransactionScope应用
  • 原文地址:https://www.cnblogs.com/yangjingru/p/13557829.html
Copyright © 2011-2022 走看看