码迷,mamicode.com
首页 > 数据库 > 详细

培训第四天-----jdbc连接oracle

时间:2016-07-21 12:35:20      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

  • oracle链接数据库并向tableone插入中一条数据
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();
    }

}
  • 查询数据库中tableone的数据
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来进行增删改查
    //用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;
        }

     

培训第四天-----jdbc连接oracle

标签:

原文地址:http://www.cnblogs.com/endingzero/p/5691267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!