首先在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()
}
}
}
}
注意,上面的ip地址什么的要填对,ZZZ_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()
}
}
}
//此类为连接数据库并进行数据库的 *** 作\x0d\x0aimportjava.sql.Connection\x0d\x0aimportjava.sql.DriverManager\x0d\x0aimportjava.sql.ResultSet\x0d\x0aimportjava.sql.SQLException\x0d\x0aimportjava.sql.Statement\x0d\x0apublicclassConn{\x0d\x0aprivatestaticConnectionconn=null\x0d\x0aprivatestaticStatementst=null\x0d\x0aprivatestaticResultSetrs=null\x0d\x0a//建立数据库的连接\x0d\x0apublicConn(){\x0d\x0aStringurl="jdbc:sqlserver://localhost:1433databaseName=ZYGX"\x0d\x0aStringuser="sa"\x0d\x0aStringpassword="123"\x0d\x0atry{\x0d\x0aClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")\x0d\x0aconn=DriverManager.getConnection(url,user,password)\x0d\x0ast=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)\x0d\x0a}catch(ClassNotFoundExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}catch(SQLExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a//通过不同的sql语句,得到相应Resultset结果集\x0d\x0apublicResultSetgetRs(Stringsql){\x0d\x0atry{\x0d\x0ars=st.executeQuery(sql)\x0d\x0a}catch(SQLExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0areturnrs\x0d\x0a}\x0d\x0a//根据不同的sql语句,执行数据库的更新 *** 作\x0d\x0apublicintupdata(Stringsql){\x0d\x0aintnum=0\x0d\x0atry{\x0d\x0anum=st.executeUpdate(sql)\x0d\x0a}catch(SQLExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0areturnnum\x0d\x0a}\x0d\x0a//关闭数据库连接相应的资源\x0d\x0apublicvoidclose(){\x0d\x0atry{\x0d\x0aif(rs!=null){\x0d\x0ars.close()\x0d\x0ars=null\x0d\x0a}\x0d\x0aif(st!=null){\x0d\x0ast.close()\x0d\x0ast=null\x0d\x0a}\x0d\x0aif(conn!=null){\x0d\x0aconn.close()\x0d\x0aconn=null\x0d\x0a}\x0d\x0a}catch(SQLExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a----------------------------------------------------------------------\x0d\x0a//可以对button里添加动作按钮:\x0d\x0afinalJButtonbutton=newJButton()\x0d\x0abutton.addActionListener(newActionListener(){\x0d\x0apublicvoidactionPerformed(finalActionEvente){\x0d\x0aConnconn=newConn()\x0d\x0aStringsql1="select*fromaawherename='"+name+"'"//按name值查找\x0d\x0aResultSetrs=conn.getRs(sql1)\x0d\x0atry{\x0d\x0awhile(rs.next()){\x0d\x0aintn=rs.getString("type")\x0d\x0a}\x0d\x0a}catch(SQLExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0aStringname=textField.getText()\x0d\x0aStringsql="updateaasettittle='"+name+"'"//从aa表将title字段的值改成textField里的name值\x0d\x0aStringsql2="deletefromaawherename='"+name+"'"//从aa表将按取得name的值删除该行数据\x0d\x0aStringsql3="insertintoaa(name,uname)values('"+name+"','"')"//将name,uname值新增到aa表\x0d\x0aif(conn.update(sql)==1){\x0d\x0aSystem.out.print("修改成功")\x0d\x0a}\x0d\x0aif(conn.update(sql2)==1){\x0d\x0aSystem.out.print("删除成功")\x0d\x0a}\x0d\x0aif(conn.update(sql3)==1){\x0d\x0aSystem.out.print("新增成功")\x0d\x0a}\x0d\x0a}\x0d\x0a})欢迎分享,转载请注明来源:内存溢出
评论列表(0条)