jdbc 基础学习

package study; import java.sql.*; import java.util.Scanner; //public class StudyJdbc { // // public static void main(String[] args) { // Connection conn = null; // Statement st = null; // ResultSet rs = null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // st = conn.createStatement(); // rs = st.executeQuery("select * from message"); // while (rs.next()) // System.out.println(rs.getInt("id") + " : " // + rs.getString("content")); // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if (rs != null) // rs.close(); // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //} //public class StudyJdbc { // // // //PreparedStatment 预处理,过滤一些特殊字符消除sql注入,效率也更高 // public static void main(String[] args) { // Connection conn = null; // PreparedStatement st = null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // st=conn.prepareStatement("insert into message values(null,?,current_timestamp)");//? 占位符 // //select * from message where id=? and content=? // st.setString(1, "你好啊"); // st.executeUpdate(); // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //} //public class StudyJdbc { // // // //批处理 // public static void main(String[] args) { // Connection conn = null; // PreparedStatement st = null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // st=conn.prepareStatement("insert into message values(null,?,current_timestamp)");//? 占位符 // // st.setString(1, "批处理1"); // st.addBatch(); // // st.setString(1, "批处理2"); // st.addBatch(); // st.setString(1, "批处理3"); // st.addBatch(); // // // st.executeBatch();//插入时三条一起 // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //} //public class StudyJdbc { // // // //事务处理 // public static void main(String[] args) { // Connection conn = null; // PreparedStatement st = null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // // conn.setAutoCommit(false);//默认自动提交 // // st=conn.prepareStatement("insert into message values(null,?,current_timestamp)");//? 占位符 // // st.setString(1, "批处理1"); // st.addBatch(); // // st.setString(1, "批处理2"); // st.addBatch(); // st.setString(1, "批处理3"); // st.addBatch(); // // st.executeBatch();//插入时三条一起 // // conn.commit();//确认提交 // conn.setAutoCommit(true);//恢复自动提交 // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // try{ // if(conn!=null) // { // conn.rollback();//出错时回滚 // conn.setAutoCommit(true); // } // } // catch(SQLException e1) // { // e1.printStackTrace(); // } // } finally { // try { // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //} //public class StudyJdbc { // // // //處理可滾動的結果集 // public static void main(String[] args) { // Connection conn = null; // Statement st = null; // ResultSet rs=null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // // //需要開發包的支持 并設置下麵的參數 // st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);//只讀,不能更新 // rs=st.executeQuery("select * from message"); // // rs.next();//不指定上面兩個參數的話,只能用rs.next(); // System.out.println(rs.getInt("id")); // rs.last(); // System.out.println(rs.getInt("id")); // //rs.previous() ; // //rs.absolute(8); // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //} //public class StudyJdbc { // // // //處理可更新的結果集 // public static void main(String[] args) { // Connection conn = null; // Statement st = null; // ResultSet rs=null; // try { // Class.forName("org.gjt.mm.mysql.Driver"); // conn = DriverManager.getConnection( // "jdbc:mysql://localhost:3306/fly", "root", ""); // // //需要開發包的支持 并設置下麵的參數 // st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);//只讀,可更新 // rs=st.executeQuery("select * from message"); // // rs.next();//不指定上面兩個參數的話,只能用rs.next(); // //更新一行数据 // rs.updateString("content", "可更新");//更新内存中数据 // rs.updateRow();//更新数据库中的内容 // // //插入新行 // rs.moveToInsertRow(); // rs.updateString("content", "AAAA"); // rs.insertRow(); // //将光标移动到新建的行 // rs.moveToInsertRow(); // // //删除行 // rs.absolute(3); // rs.deleteRow(); // // } catch (ClassNotFoundException e) { // e.printStackTrace(); //写到错误日志里 // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if (st != null) // st.close(); // if (conn != null) // conn.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } //在eclipse中查看数据库中的内容 //}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值