import javasql;
public class DBCon {
public DBCon()
{
}
public Connection getConnection()
{
Connection conn=null;
try{
ClassforName("oraclejdbcdriverOracleDriver");//驱动程序名
String url="jdbc:oracle:thin:@192168151:1521:evatesoft";
//192168151为服务器的地址
//ads2为数据库的SID ads2
String user="evatesoft";//数据库的用户名
String password="evatesoft";//数据库的密码
conn= DriverManagergetConnection(url,user,password);
}
catch(Exception e)
{
Systemoutprintln(etoString());
}
return conn;
}
}
在jsp中<%DBCon db = new DBCon(); %>
1先装好JDK ex:C:\j2sdk142
2再装TomCat ex:C:\Tomcat 50
3下载 JDBC 驱动
>
首先是数据库连接代码类:
然后在你的jsp页面写上调用数据连接类的增删改查就可以了。
不懂hi我
jsp页面中
<%
String sqlgetServiceId="select eid from eip_service e where eservice_name_en='"+serviceNamesubstring(serviceNamelastIndexOf("_")+1)+"' and eservice_version=10";
int sid=BaseDBqueryId(sqlgetServiceId, null);
%>
BaseDBjava
import javasqlConnection;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilregexMatcher;
import javautilregexPattern;
public class BaseDB {
public static String URL = "jdbc:oracle:thin:@192168174189:1521:soadb"; //版本管理ERP数据库配置
public static String NAME = "SVMDEV";//用户名
public static String PWD = "SVMPWD";//密码
public static PreparedStatement ps =null;
public static ResultSet rs =null;
public static Connection connection=null;
//获取数据库连接信息
public static Connection getConnection() {
try {
ClassforName("oraclejdbcOracleDriver");
if (connection==null) {
connection=DriverManagergetConnection(URL, NAME, PWD);
}
} catch (ClassNotFoundException e) {
eprintStackTrace();
} catch (SQLException e) {
eprintStackTrace();
}
return connection;
}
//查询数据,根据相关信息查询得到当前服务的某个需要的id
public static int queryId(String sql,String parameter[] ) {
int getId=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 1; i <=parameterlength; i++) {
pssetString(i,parameter[i-1]);
}
}
rs=psexecuteQuery();
if(rsnext()&&rs!=null){
getId=rsgetInt(1);
}
} catch (SQLException e) {
eprintStackTrace();
}finally{
closeAll(ps, rs, connection);
}
return getId;
}
//修改数据
public static int updateData(String sql,String parameter[] ) {
int count=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 1; i <=parameterlength; i++) {
pssetString(i,parameter[i-1]);
}
}
count=psexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
}finally{
closeAll(ps, rs, connection);
}
return count;
}
//插入数据
public static int insertData(String sql,String parameter[]) {
int num=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 0; i <parameterlength; i++) {
pssetString(i+1,parameter[i]);
}
}
num=psexecuteUpdate();
} catch (Exception e) {
eprintStackTrace();
}finally{
closeAll(ps,null,connection);
}
return num;
}
//关闭所有
public static void closeAll(PreparedStatement ps,ResultSet rs,Connection connection) {
try {
if (ps!=null) {
psclose();
}
} catch (Exception e2) {
try {
if (rs!=null) {
rsclose();
rs=null;
}
} catch (Exception e3) {
try {
if (connection!=null) {
//connectionclose();
//connection=null;
}
} catch (Exception e4) {
e4printStackTrace();
}
}
}
}
}
以上就是关于Jsp中数据库的连接怎么做全部的内容,包括:Jsp中数据库的连接怎么做、请问怎么用jsp连接mysql数据库.、如何把JSP数据写到数据库中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)