java数据库

来源:互联网 发布:iphone8怎么卸载软件 编辑:程序博客网 时间:2024/05/17 09:23

packagelsh;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.SQLException;

publicclassaccessLSH {

publicstatic Connection connlsh = null;

publicstaticResultSetlh=null;

publicaccessLSH(){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connlsh=DriverManager.getConnection("jdbc:odbc:ll");

System.out.println("链接成功");

}catch(Exception e){

e.printStackTrace();

System.out.println("链接不成功");

}

}

publicResultSetchaxun(String sqllsh){

lh=null;

try{

lh=connlsh.createStatement().executeQuery(sqllsh);

}catch(SQLExceptione){

e.printStackTrace();

}

returnlh;

}

publicintgengxin(String sqllsh){

intr=0;

try{

r=connlsh.createStatement().executeUpdate(sqllsh);

}catch(SQLExceptione){

e.printStackTrace();

}

returnr;

}

publicstaticvoidguanbi(){

if(connlsh!=null){

try{

connlsh.close();

connlsh=null;

}catch(SQLExceptione){

e.printStackTrace();

System.out.println("---Exception--->>关闭"+e.getMessage()); }

}

}

publicstaticvoid main(String[] args) {

accessLSHsh=newaccessLSH();

String sqllsh="insert into lsh(id,xingming,nianling)values('1','梁山华','20')";

inti=sh.gengxin(sqllsh);

System.out.println("插入"+i+"行"); sqllsh="select * from lsh"; sh.chaxun(sqllsh); try{ while(sh..next()){ System.out.println("编号:"+sh..getInt(1)); System.out.println("姓名:"+sh..getString(2)); System.out.println("年龄:"+sh..getString(3)); } }catch(SQLExceptione){ e.printStackTrace(); }

}

}

Java免费学习资料直播公开课加老师QQ578024144