如果不用ORM框架,那么直接引入ojdbc6.jar 驱动包,然后在代码中JDBC配置
然后可以使用jdbc实现增删改查,比如下面代码
ResultSet rs = nullStatement stmt = null
Connection conn = null
try {
Class.forName("oracle.jdbc.driver.oracleDriver")
//new oracle.jdbc.driver.oracleDriver()
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:yuewei", "scott", "tiger")
stmt = conn.createStatement()
rs = stmt.executeQuery("select * from dept")
while(rs.next()) {
System.out.println(rs.getString("deptno"))
//System.out.println(rs.getInt("deptno"))
}
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
} finally {
try {
if(rs != null) {
rs.close()
rs = null
}
if(stmt != null) {
stmt.close()
stmt = null
}
if(conn != null) {
conn.close()
conn = null
}
} catch (SQLException e) {
e.printStackTrace()
}
}
2.
增删改查都有链接网页链接
粘贴着查询的:
package cn.web.jdbc
import java.sql.*
public class UserLogin {
public static void main(String[] args) {
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver")
// 获取连接
String url = "jdbc:mysql://localhost:3306/usejdbc?useUnicode=true&characterEncoding=UTF-8&useSSL=false"
String user = "root"
String mysqlPassword = "123456"
//模拟前台传入的用户名和密码
String InputUsername = "老八"
String InputPassword = "123456"
try {
// 连接对象输入三个参数
Connection connection = DriverManager.getConnection(url, user, mysqlPassword)
System.out.println(connection)
//定义sql语句
// 查询
String sql1 = "select * from student where username='" + InputUsername + "' and password='" + InputPassword + "'"
System.out.println(sql1)
Statement statement = connection.createStatement()
ResultSet resultSet = statement.executeQuery(sql1)
System.out.println(resultSet)
if (resultSet.next()) {
System.out.println("登录成功")
} else {
System.out.println("登录失败")
}
// 释放资源
statement.close()
connection.close()
resultSet.close()
} catch (SQLException e) {
e.printStackTrace()
}
} catch (ClassNotFoundException e) {
e.printStackTrace()
}
}
}
首先在oracle文件下找到jdbc文件,里面的lib文件下有个class12.zip在eclipse里新建个项目,建好后在左边右击那个项目图标,选择属性(最下面的),里面有个java build path,点进去,找到add external jars按键,点一下,把那个class12.zip加载进来,然后可以开始写代码了。
我的代码如下:
import java.sql.*
public class connect {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.driver.OracleDriver")
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
Connection con = null
Statement stmt = null
ResultSet rs = null
try{
String url="jdbc:oracle:thin:@服务器ip地址:端口号:数据库名字"
String user="用户名"
String password="密码"
String str="INSERT INTO ZZZ_2 VALUES('041110018','JHDK')"
con = java.sql.DriverManager.getConnection(url,user,password)
// 创建状态
stmt = con.createStatement()
// 执行SQL语句,返回结果集
//int rowcount = stmt.executeUpdate(str)
int j = stmt.executeUpdate("update ZZZ_2 set NAME='dbt' where ID=21")
int k = stmt.executeUpdate("delete from ZZZ_2 where ID=41110020")
rs = stmt.executeQuery("SELECT * FROM ZZZ_2")
// 对结果集进行处理
while (rs.next()) {
int id = rs.getInt("ID")
String name = rs.getString("NAME")
//Integer age = rs.getObject("age") == null ? null : rs.getInt("age")
System.out.println(id + ": " + name)
} }catch(SQLException e){
e.printStackTrace()}
// 释放资源
finally{
try{
rs.close()
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
try{
stmt.close()
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
try{
con.close()
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)