欢迎来到天天文库
浏览记录
ID:70879324
大小:13.60 KB
页数:1页
时间:2021-11-28
《java连接数据库》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagedb;importjava.sql.*;publicclassdb{privateConnectiondbConn;privateStatementstateMent;publicdb(){StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";StringdbURL="jdbc:sqlserver://localhost:1433;Databasename=student";StringuserName="sa";Stringuse
2、rpwd="123";try{Class.forName(driverName);dbConn=DriverManager.getConnection(dbURL,userName,userpwd);System.out.println("ConnectionSuccessful!");}catch(Exceptione){System.out.println(e.getMessage());}}publicintexecuteUpdate(Stringsql)throwsSQLException{stateMent=dbConn.cre
3、ateStatement();returnstateMent.executeUpdate(sql);}publicResultSetexecuteQuery(Stringsql)throwsSQLException{stateMent=dbConn.createStatement();returnstateMent.executeQuery(sql);}publicvoidcloseConn()throwsSQLException{stateMent.close();dbConn.close();}publicPreparedStatementPreparedStatement
4、(Stringsql)throwsSQLException{returndbConn.prepareStatement(sql);}}
此文档下载收益归作者所有