怎么获得数据库DSN名称

怎么获得数据库DSN名称,第1张

用JDBC连接数据库,然后用sql语句。要导入mysql的驱动包。

importjavasql;

publicclassTestMySql{

staticConnectioncon=null;//声明Connection对象

staticStatementsql=null;

staticResultSetres=null;

publicstaticvoidmain(String[]args){

TestMySqlc=newTestMySql();

con=c();

try{

sql=con();

res=sql("selectfromdept");

//sql语句,我数据库里有张dept表

while(resnext()){//输出结果

Systemoutprint(resgetString(1)"

Systemoutprint(resgetString(2)"

Systemoutprint(resgetString(3));

Systemoutprintln();

}

}catch(e){

e();

}finally{

try{

if(res!=null){

resclose();

res=null;

}

if(sql!=null){

sqlclose();

sql=null;

}

if(con!=null){

conclose();

con=null;

}

}catch(e){

e();

}

}

}

publicConnection(){

try{

ClassforName("commysqljdbcDriver");

//加载oracleJDBC驱动

Systemoutprintln("数据库驱动加载成功");

}catch(e){

e();

}

try{//通过访问数据库的URL获取数据库连接对象

con=(

"jdbc:mysql://localhost:3306/mydata","root","qwer1234");

//mydata为mysql名字

Systemoutprintln("数据库连接成功");

}catch(e){

e();

}

returncon;//按方法要求返回一个Connection对象

}

}

以上就是关于怎么获得数据库DSN名称全部的内容,包括:怎么获得数据库DSN名称、数据库怎么获得什么是数据库、java中怎么获取mysql数据库的数据(java获取mysql数据库列表)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10158042.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存