jdbc

连接数据库,控制台查询

1.加载驱动 Class.forName()
2. 建立连接Connection conn= DriverManager.getConnection(url,user,password);
3.创建查询对象 Statement st= conn.createStatement
4.执行 int = executeUpdate()
ResultSet rs= executeQuery()

`**

 出现的问题                1. 加载驱动写错   oracle.jdbc.driver.OracleDriver
         2. @ip 地址         @不能少
      3.控制台  int len= System.in.read(数组);   读取 出来 sql=new String(数组,0,len);  
        4.先去除 所有列数量   再通过游标查询   while(rs.next) 遍历所有列 rs.getString();
         5.列 序列 从1开始 不是----------0开始     
         6.记得换行  
         7.   

public class Conn {
public static void main(String[] args) throws Exception, SQLException, IOException {
// 1. 加载驱动 区分厂商
Class.forName(“oracle.jdbc.driver.OracleDriver”);
// 2. 数据库建立连接 ,端口进程在听 地址表示电脑,端口表示进程,数据库名字 (tcp连接)
Connection conn = DriverManager.getConnection(“jdbc:oracle:thin:@192.168.180.100:1521:zsz”, “yt”, “yt123”);

	// 3.查询对象
	Statement st = conn.createStatement();

// 控制台读取 空字符 String sql=null;
byte[] b = new byte[200];
String sql = null;
while (true) {
System.out.println(“请输入命令:”);
int len = System.in.read(b);

		if (len == 2)
			continue;
		sql = new String(b, 0, len-2);

		if (sql.equals("exit") || sql.equals("quit")) {
			System.exit(0);
		}

		if (sql.contains("select")) {
			ResultSet rs = st.executeQuery(sql); //   executeQuery 结果  ResulSet 类型

			int fldCnt = rs.getMetaData().getColumnCount();
			for (int i = 1; i <= fldCnt; i++) {
				System.out.print(rs.getMetaData().getColumnName(i) + "\t");
			}
			System.err.println();

			while (rs.next()) {
				for (int i = 1; i <= fldCnt; i++) {
					System.out.print(rs.getString(i) + "\t");
				}
				System.out.println();
			}
		} else {
			st.executeUpdate(sql);// 结果int  类型
		}
	}
}

}

向数据库插入数据,

 ****第一步oracle中创建表格 ypl ,**
       create table ypl(ypla int ,yplb varchar(20),yplc float,ypld varchar(20) );**

创建 包 com.model(存放表 字段数据 )

    定义类 Ypl           (和表对应 )
  public class Ypl{
           private int ypla;
           private String yplb;
           private float yplc;
           private String ypld;       
           // 构造 有参 无参数 , get,set方法 
     public int getYpla() {
                	return ypla;
}
public void setYpla(int ypla) {
	this.ypla = ypla;
}
public String getYplb() {
	return yplb;
}
public void setYplb(String yplb) {
	this.yplb = yplb;
}
public float getYplc() {
	return yplc;
}
public void setYplc(float yplc) {
	this.yplc = yplc;
}
public String getYpld() {
	return ypld;
}
public void setYpld(String ypld) {
	this.ypld = ypld;
}
public Ypl(int ypla, String yplb, float yplc, String ypld) {
	super();
	this.ypla = ypla;
	this.yplb = yplb;
	this.yplc = yplc;
	this.ypld = ypld;
}
public Ypl() {
	super();
}
  @Override
public String toString() {
	// TODO Auto-generated method stub
	return super.toString();
     }
}

创建com.dao (存放和数据库打交道的类,
定义三个函数,
1.连接数据库
**2.增加数据到List数组中
3. 执行exeUpdate语句

   public  class YplDao{
      private Connection getConn(){
          Connection con=null;
           try{
             // **加载驱动**
               Class.forName("oracle.jdbc.driver.OracleDriver");
              // 建立连接 
              String url="jdbc:oracle:thin:@192.168.94.128:1521:zsz";
              String user="yt";
              String password="yt123";
     
             con=  DriverManager.getConnection(url,user,password);  // 当时少了con
           }catch(Exception e){
              System.err.println(e.getMessage());
           }
            return con;
      }
      
 //  2. 创建存放数据的函数 到List数组中
    public List<Ypl> getData(String sql){
           List<Ypl> ypl=new ArrayList<>();  // 当时 犯错 把 new ArrayList<添加了Ypl> 
          try{
                Connection con=getConn();
                Statement st= con.createStatement();// 创建查询对象
                 ResultSet rs=  st.executeQuery(sql); // 将结果赋给游标  当存在即rs.next()
           while(rs.next()){
                Ypl y=new Ypl();
                y.setYpla(rs.getInt("ypla"));
                y.setYplb(rs.getString("yplb"));
                y.setYplc(rs.getFloat("yplc"));
                y.setYpld(rs.getString("ypld"));
                ypl.add(y); // 把 表中数据 添加到 List数组中 
           }
          }catch(Exception e){
              System.err.println(e.getMessage());
          } 
            return ypl;
    }

  // 创建update函数 
     public boolean exeUpdate(String sql){
          try{
              Connection con=getConn();
                Statement st= con.createStatement();
                 st.executeUpdate(sql);   
          }catch(Exception e){
             return false;
          }
            return true;
     }
    public static void   main(String args[]){
         YplDao dao=new YplDao();
       boolean flag= dao.exeUpdate("insert into ypl values(6,6,6,6)");
      System.out.println(flag);
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值