java简单学习笔记20181219

mysql的安装,java平台connector数据库驱动的下载及引用,

数据库连接器的动态载入注册Class.forName("com.mysql.jdbc.Driver"),

之后,会自动被DriverManager注册后,可使用DriverManager.getConnection(url,user,password)得到connection对象,

其中url为数据库的连接字符串

MySQL=》 jdbc:mysql://localhost:3306/mydb&useSSL=false&user=root&password=root

Oracle=》jdbc:oracle:thin:@localhost:1521:orcl

MSSQLServer=》 jdbc:microsoft:sqlserver://localhost:1433;databasename=dbname

Statement对象的创建Statemenet smt=conn.createStatement(),执行sql语句smt.executeQuery(),返回结果集ResultSet,execute(),.executeupdate()返回受响的行数

PreparedStatement对象的创建,conn.prepareStatement(“select * from mytable where id=? and name=?)语句中用?问号作为占位符,prestatement.setInt(x),pprestatement.setString(xxx)等根据数据类型 的、prestatment.setXXX(v)的向sql语句中的参数占位符赋值。

使用prepastatment.executeQuery()等执行。

ResultSet对象是Statement和PrepareStatement的执行结果集,使用rs.next()移动行,并用rs.getString(列名或列号)取行的列值,rs.getInt(x),rs.getDate(x)等rs.getXXX(name)对应数据类型的值。

ResultSet对象的updateByte(列号,值),rs.updateString(列号,值),只是对结果集中临时存放的数据进行了修改,之后要用rs.updateRow()才能修改对应的物理数据表中的值,

rs.movetoinsertrow()移动到新增行,再用rs.updateXXX(列号,值)修改后,rs.insertRow()新行的增加提交到物理表中。

猜你喜欢

转载自www.cnblogs.com/lofe/p/10146255.html