培训第四天-----jdbc连接oracle
- 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;
}解决了当对象没有被NEW 的时候,去setid会出现空指针。
public userTableDaoBean selector(String where) {
PreparedStatement ps;
userTableDaoBean ub = new userTableDaoBean();
try {
String sql = "select * from usertable where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(where));
ResultSet rs = ps.executeQuery();
if(rs.next()){
ub.setId(rs.getInt("id"));
ub.setName(rs.getString("name"));
ub.setSex(rs.getString("sex"));
ub.setAge(rs.getString("age"));
}
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ub;
}
像这个方法,new userTableDaoBean()如果写成null,容易出现空指针。
培训第四天-----jdbc连接oracle的更多相关文章
- jdbc连接oracle数据库
/*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...
- Linux(Centos)下jdbc连接oracle速度超慢的问题
最近在centos下写个java swing程序,发现在linux用jdbc连接oracle及其缓慢,还经常失败.但是同样的程序在windows下运行就连接的非常快.网上搜索了很长时间都和我这情况没关 ...
- jdbc 连接 oracle rac
jdbc 连接 oracle rac 的连接串如下: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192. ...
- JDBC连接Oracle数据库的问题
场景:最近做一个java web项目,使用jdbc连接Oracle数据库,遇到了两个问题. 问题1:jdbc连接不上Ubuntu Oracle服务器? 后来发现这个问题的原因是由于连接字符串写错了,修 ...
- JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
转载至http://www.blogjava.net/itspy/archive/2007/12/20/169072.html Oracle 问题描述:今天使用jdbc连接oracle 10.2.0. ...
- jdbc连接oracle数据库问题
下面是JDBC连接oracle数据库流程: String dbURL = "jdbc:oracle:thin:@url:1521:service_name"; String use ...
- JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库
首先是JDBC 连接Oracle 数据库 package com.util; import com.pojo.UserInfo; import java.sql.*; public class DB ...
- JAVA通过JDBC连接Oracle数据库详解【转载】
JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...
- JDBC连接ORACLE无法登陆java.sql.SQLException: ORA-01017: invalid username/password; logon denied
当用jdbc连接Oracle数据库的时候 private Connection getConnection() throws SQLException { OracleDataSource ods = ...
随机推荐
- kafka性能基准测试
转载请注明出处:http://www.cnblogs.com/xiaodf/ 1.测试环境 该benchmark用到了六台机器,机器配置如下 l IntelXeon 2.5 GHz processo ...
- 基于CSS的幻灯片工具 reveal.js
官网:http://lab.hakim.se/reveal-js/#/ github https://github.com/hakimel/reveal.js 更多资源:6个最好的 HTML5/CS ...
- 【转载】Android 自动化测试 Emmagee
Emmagee 是一个性能测试小工具 用来监控指定被测应用在使用过程中占用机器的CPU, 内存,流量资源的性能小工具 Emmagee 介绍 Emmagee是网易杭州研究院QA团队开发的一个简单易上手的 ...
- 新建STM32工程
1) 2)保存 3)选择公司和芯片的型号,STM32F103C8T6,64kB Flash, 20kB SRAM. 4)手动添加启动代码 5)新建如下文件夹 6)回到工程,选中target,右键Add ...
- Install Debian note
environment: already install xp,win7 and win8PE, left 87G space to install Debian, this space not fo ...
- [Note] Build your SDL2 Environment in Visual Studio 2013 配置你的SDL2运行环境
Right key your project in "solution manager(解决方案资源管理器)", choose the "Property(属性)&quo ...
- Python面向对象详解
Python面向对象的"怜人之处" Python的待客之道--谁能进来 Python的封装--只给你想要的 Python的继承--到处认干爹 Python的多态--说是就是
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 初识linux端c++程序开发
关于linux端程序开发,我以前一直不知道是做些什么,只是感觉听高端的.最近接触了一些,有了一些初步的认识. 首先,linux是一个操作系统,跟windows一样:接通电源.按下电脑开机,电脑就会自动 ...
- JSPatch – 动态更新iOS APP
原文:http://blog.cnbang.net/works/2767/ JSPatch是最近业余做的项目,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的 ...