标签:
package com.zjw.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class mydbbase { private Connection conn; private String oracleUrl="jdbc:oracle:thin:@localhost:1521:orcl"; //private String mysqlUrl="jdbc:mysql://localhost:3306//mysql"; private String username = "u1"; private String password = "Admin123"; public mydbbase(){ try { Class.forName("oracle.jdbc.driver.OracleDriver"); DriverManager.setLoginTimeout(0); conn = DriverManager.getConnection(oracleUrl,username,password); Statement stmt = conn.createStatement(); String sql = "insert into tableone(id,name,sex,age) values(1,‘jiewei‘,‘男‘,‘23‘) "; stmt.executeUpdate(sql); conn.commit(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { mydbbase mb = new mydbbase(); } }
package com.zjw.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class mydbbase { private Connection conn; private String oracleUrl="jdbc:oracle:thin:@localhost:1521:orcl"; //private String mysqlUrl="jdbc:mysql://localhost:3306//mysql"; private String username = "u1"; private String password = "Admin123"; public mydbbase(){ try { Class.forName("oracle.jdbc.driver.OracleDriver"); DriverManager.setLoginTimeout(0); conn = DriverManager.getConnection(oracleUrl,username,password); Statement stmt = conn.createStatement(); //插入语句 //String sql = "insert into tableone(id,name,sex,age) values(1,‘jiewei‘,‘男‘,‘23‘) "; //String sql = "delete from tableone where id=1"; //stmt.executeUpdate(sql); //查询语句 String sqlSelect = "select * from tableone"; ResultSet rs = stmt.executeQuery(sqlSelect); while(rs.next()){ System.out.print(rs.getInt("id")+" "); System.out.print(rs.getString("name")+" "); System.out.print(rs.getString("sex")+" "); System.out.print(rs.getString("age")+" "); } conn.commit(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { mydbbase mb = new mydbbase(); } }
//用preparedStatement来进行增删改查 public static boolean auth(String name,String password){ conn = getConnection(); try { PreparedStatement pstmt = conn.prepareStatement("select * from tableone " + "where name=? and password = ?"); pstmt.setString(1, name); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ return true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
标签:
原文地址:http://www.cnblogs.com/endingzero/p/5691267.html