标签:date 方式 cti code database 简单操作 abs sys last
链接数据库
public static void main(String[] args) { //简约方式 try { Connection conn = DriverManager.getConnection("jdbc:mysql:/mysql?user=aa"); Statement st = conn.createStatement(); ResultSet rs =st.executeQuery("show databases"); while (rs.next()){ System.out.println(rs.getString(1)); } rs =st.executeQuery("select version()");//结果集默认是开始前一个 rs.next(); System.out.println(rs.getString(1)); rs = st.executeQuery("select *from db1.stu"); while (rs.next()){ System.out.printf("学号:%d,姓名:%s,成绩:%d分 \n",rs.getInt(1),rs.getString("name"),rs.getShort("score")); } rs.close(); st.close(); conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } }
Connection 接口
public class StatementDemo { public static void main(String[] args) { try { Connection c =DriverManager.getConnection("jdbc:mysql:/db1?user=aa"); Statement s =c.createStatement(); System.out.println(s.execute("show tables"));//execute一般执行操作 建库、建表、删除 //execute 有影响行、有结果的返回true值 ,没有的返回false Exception //对数据操作 //ResultSet rs =s.executeQuery("select show");//返回结果集,查询时,返回ResultSet 插入 删除 修改 //int rows= s.executeUpdate("insert datete update ");//返回影响行、查询 //插入数据 // int n = s.executeUpdate("insert stu values (null ,‘李四‘,30),(null,‘aa‘,50),(null,‘mm‘,90)"); // System.out.println(n); //删除数据 // int n =s.executeUpdate("delete from stu where id in(12)"); //修改表 // int up =s.executeUpdate("update stu set score=score+5"); // System.out.println(up); //批量语句 insert update delete 不能有查询语句select、crete datebesea // s.addBatch("insert into stu values(null ,‘ttt‘,30),(null,‘jaav‘,98)"); // // s.addBatch("delete from stu where id=10"); // s.addBatch("delete from stu where id=2"); int[] ns = s.executeBatch(); for (int i:ns){ System.out.println(i); } //查询select show select 1,2,3; // ResultSet rs = s.executeQuery("select 1,2,3*3"); ResultSet rs =s.executeQuery("select *from stu"); // rs.next(); while (rs.next()) { System.out.printf("id:%d,name:%s,score:%d\n", rs.getInt(1), rs.getString(2), rs.getInt(3)); } rs.close(); s.close(); c.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
ResultSet,浮标
public class StatementDemo2 { public static void main(String[] args) { try { Connection c = DriverManager.getConnection("jdbc:mysql:/db1?user=aa"); Statement s =c.createStatement(); ResultSet rs = s.executeQuery("select version()"); rs.next(); System.out.println(rs.getString(1)); // rs = s.executeQuery("select name,id,score as ss from stu where true order by score desc"); Statement q = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);//添加游标操作 rs = q.executeQuery("select name,id,score as ss from stu order by score desc"); boolean ok = rs.isBeforeFirst(); if(ok){ System.out.println("有数据"); rs.next(); rs.next(); System.out.println(rs.getString("id")); rs.previous(); System.out.println(rs.getString("id")); rs.last(); System.out.println(rs.isLast()); System.out.println(rs.getString("id")); rs.absolute(4); //false 如果没有400条,指针指向最后一条记录之后。*跳转 rs.previous(); rs.previous(); System.out.println(rs.isLast());//看是不是在最后 System.out.println(rs.isAfterLast());//最后之后 System.out.println("-".repeat(50)); while(rs.next()){//*调转后从这里输出完 System.out.println(rs.getString("id")); } }else{ System.out.println("没有结果"); } // while(rs.next()){ // System.out.printf("id:%d,name:%s,score:%d。\n",rs.getInt(2),rs.getString("name"),rs.getInt("ss")); // } // // // // // // System.out.println(rs.isBeforeFirst());//第一条记录之前 // System.out.println(rs.isFirst());//首先 // System.out.println(rs.isLast());//最后 // System.out.println(rs.isAfterLast());//在最后之后 // System.out.println(rs.next());//下一个 // System.out.println(rs.previous());//以前的 // System.out.println(rs.absolute(6));//绝对 // System.out.println(rs.first());//第一个 } catch (SQLException throwables) { throwables.printStackTrace(); } } }
jdb链接数据库,简单操作(ResultSet,浮标)(DriverManager 驱动管理器类 Connection 接口 Statement 接口)
标签:date 方式 cti code database 简单操作 abs sys last
原文地址:https://www.cnblogs.com/Gu1015/p/14462926.html