关键代码:
public Connection connecting(){
try{
ClassforName("oraclejdbcdriverOracleDriver");
}catch(ClassNotFoundException e){
eprintStackTrace();
}
try{
connect=DriverManagergetConnection("jdbc:oracle:thin:SYSTEM@//localhost:1521/ORCL","SCOTT","tiger");//SYSTEM@//localhost:1521/ORCL为数据库链接地址,Scott用户名,tiger为密码//
}catch(SQLException e){
eprintStackTrace();
}
return connect;
}
只能写个大概的,要写数据到数据库中,先得在数据库中建库,库里建表,表里建字段,然后java里建立数据库连接,用SQL语言写数据到表中的字段
ClassforName("commicrosoftsqlserverjdbcSQLServerDriver")newInstance();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名"; //70、2000
String url="jdbc:sqlserver://localhost:1433;DatabaseName=数据库名"; //2005
Connection conn=null;
conn= DriverManagergetConnection(url,用户名,密码);
PreparedStatement pst=null;
pst=connprepareStatement("Insert Into grade(表名) Values ()");
pstsetInt(1,你要写的整弄数据);
//pstsetString(2,你要写的字符串数据);
pstaddBatch();
pstexecuteBatch();
把JDBC接口放在静态代码块static{}里边就可以达到只开启一次就可以了
publicclassConnUtil{
static{
try{
ClassforName("commysqljdbcDriver");//链接数据库驱动
}catch(e){
e();
}
}
publicstaticConnection()
throws{
Connectionconn=(
//链接路径
"jdbc:mysql://localhost:3306/tarena","root","1234");
returnconn;
}
publicstaticvoid(
//关闭数据库
Connectionconn)throws{
connclose();
}
Connection con = null;//创建一个边接对象
String name = requestgetParameter("account");//用户名
String pwd = requestgetParameter("password");//密码
Statement sta = null;//SQL语句对象
ResultSet rs = null;//结果集
try {
ClassforName("commysqljdbcDriver");//加载驱动
con = DriverManagergetConnection(
"jdbc:mysql://localhost:3306/siben", "root", "root"); //获得连接
sta = concreateStatement();//获得SQL语句对象
rs = staexecuteQuery("select from siben where Account='"//执行sql语句,并返回结集
+ name + "' and password='" + pwd + "'");
if (rsnext()) {//判断是否有结果
if (rsgetString("account")equals("jonw000")//rsgetString("account")是取值
&& rsgetString("password")equals("000000"))
outprintln("登录成功");
} else
outprintln("登录失败");
} catch (Exception e) {
Systemoutprintln(etoString());
outprintln("连接失败");
}
用这个类吧好的话,给我加加分
import javasql;
/
@功能: 一个JDBC的本地化API连接类,封装了数据 *** 作方法,只用传一个SQL语句即可
@作者: 李开欢
@日期: 2007/
/
public class ConnectionDemo {
/
这里可以将常量全部放入另一个类中,以方便修改
/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "commicrosoftjdbcsqlserverSQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemogetConnection();
}
public static Connection getConnection(){
Systemoutprintln("连接中");
try {
ClassforName(ConnectionDemoDRIVER);
conn = DriverManagergetConnection(ConnectionDemoURL, ConnectionDemoUSER, ConnectionDemoPASS);
Systemoutprintln("成功连接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
Systemoutprintln("执行SQL语句中");
try {
ps = conncreateStatement(ResultSetTYPE_SCROLL_SENSITIVE, ResultSetCONCUR_UPDATABLE);
if(sqlsubstring(0, 6)equals("select")){
rs = psexecuteQuery(sql);
Systemoutprintln("执行完查询 *** 作,结果已返回ResultSet集合");
}else if(sqlsubstring(0, 6)equals("delete")){
psexecuteUpdate(sql);
Systemoutprintln("已执行完毕删除 *** 作");
}else if(sqlsubstring(0, 6)equals("insert")){
psexecuteUpdate(sql);
Systemoutprintln("已执行完毕增加 *** 作");
}else{
psexecuteUpdate(sql);
Systemoutprintln("已执行完毕更新 *** 作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
Systemoutprintln("查询结果为:");
return rs;
}
public static void closeConnection(){
Systemoutprintln("关闭连接中");
try {
if (rs != null) {
rsclose();
Systemoutprintln("已关闭ResultSet");
}
if (ps != null) {
psclose();
Systemoutprintln("已关闭Statement");
}
if (conn != null) {
connclose();
Systemoutprintln("已关闭Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemogetConnection();
String sql = "delete from type where id = 1";
ConnectionDemogetStatement(sql);
String sql2 = "insert into type values(1,'教学设备')";
ConnectionDemogetStatement(sql2);
String sql1 = "select from type";
ConnectionDemogetStatement(sql1);
ResultSet rs = ConnectionDemogetResultSet();
Systemoutprintln("编号 "+"类 型");
try {
while(rsnext()){
Systemoutprint(" "+rsgetInt(1)+" ");
Systemoutprintln(rsgetString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
ConnectionDemocloseConnection();
}
}
以上就是关于如何截获jdbc发送给数据库的sql全部的内容,包括:如何截获jdbc发送给数据库的sql、用java怎样把数据存到数据库中、java中使用JDBC连接数据库的步骤是什么(javajdbc连接oracle数据库步骤)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)