当前页面: 开发资料首页 → JSP 专题 → 这个代码看不明白,我不知道数据库如何调用,那位朋友给指点一下
这个代码看不明白,我不知道数据库如何调用,那位朋友给指点一下
摘要: 这个代码看不明白,我不知道数据库如何调用,那位朋友给指点一下
<%@ page import= "java.sql.*,javax.sql.*,java.util.*,javax.naming.*"%>
<%!
String replace(String str){
if(str==null) return "";
String strTemp="";
for(int i=0;i
String c=str.substring(i,i+1);
if(c.equals("'") || c.equals("/"")) strTemp=strTemp+"";
else strTemp=strTemp+c;
}
return strTemp;
}
String format(String str){
if(str==null) return "";
StringTokenizer strToken=new StringTokenizer(str,"<",true);
String Content_temp = "";
String makeContent = "";
while(strToken.hasMoreTokens()){
Content_temp=strToken.nextToken();
if(Content_temp.equals("<")) makeContent=makeContent+"<";
else {
makeContent=makeContent+Content_temp.substring(0,Content_temp.indexOf(">")+1);
Content_temp=Content_temp.substring(Content_temp.indexOf(">")+1,Content_temp.length());
if(!Content_temp.equals("/r/n")) Content_temp=format2(Content_temp);
makeContent=makeContent+Content_temp;
}
}
str=makeContent;
return str;
}
String format2(String str){
if(str==null) return "";
else{
StringTokenizer strToken=new StringTokenizer(str,"/n",true);
String Content_temp = "";
String makeContent = "";
while(strToken.hasMoreTokens()){
Content_temp=strToken.nextToken();
if(Content_temp.equals("/n")) makeContent=makeContent+"
";
else makeContent=makeContent+Content_temp;
}
str=makeContent;
strToken=new StringTokenizer(str," ",true);
Content_temp = "";
makeContent = "";
while(strToken.hasMoreTokens()){
Content_temp=strToken.nextToken();
if(Content_temp.equals(" ")) makeContent=makeContent+" ";
else makeContent=makeContent+Content_temp;
}
str=makeContent;
return str;
}
}
%>
<%
Connection conn=null;
Statement stmt=null;
PreparedStatement pstmt=null;
//一般情况使用
String sql="";
ResultSet rs=null;
//权限验证使用
String powerSQL="";
ResultSet powerRS=null;
//其它常用的一些变量
int i=0,j=0,k=0;
String N1="",N2="",N3="",N4="",N5="",N6="",N7="",N8="",N9="",N10="";
String strTemp="";
//分页使用的变量
int pageSize=20;//每页显示记录数
int recordCount=0;//记录总数
int pageCount=1;//总页数
int curPage=1;//当前页码
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:jinyun","sa","310030");
if(conn == null) return;
conn.setAutoCommit(false);
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
%>
==================================================
<%@ include file="/includes/base.jsp"%>
页面是这样调用的,但是会出错,我知道是sql server数据库,不过看不明白数据库源是怎么指定的
conn=DriverManager.getConnection("jdbc:odbc:jinyun","sa","310030");
这句应该是,问题是没有数据库名,怎么都感觉是少了一个
那位能给指点一下,在线讨论
conn=DriverManager.getConnection("jdbc:odbc:jinyun","sa","310030");
那个jinyun就是数据库名
jinyun这就是个数据源阿!他这就采用jdbc-odbc桥的方式进行连接的!
那么数据源呢?怎么没有看到?我不知道那个是数据源
localhost:1433这样的东西
楼上帮我说了。。
他是在控制面版的管理工具里面的数据源那把数据库就已经配置好了!所以不需要配置数据库!
jinyun这就是个数据源阿!他这就采用jdbc-odbc桥的方式进行连接的!
什么叫做桥方式?我怎么配置能用?
是odbc的方式啊,就算是odbc,那么也应该有一个连接源啊
还有,这个能直接修改成连接池那样的方式吗?可以用吗?
控制面板==>管理工具==>数据源
从那里配置
连接有好多方式阿!jdbc-odbc桥是一种方式。还有本地API也是一种方式!
还有一些第三方的一些连接方式!!具体有好多种我也不太清楚!!
你说改成哪中连接池的方式阿!
你在开始运行那里输入odbcad32.exe就可以添加数据源了!
如何配置是明白的,难道是那个jinyun就是odbc的数据源名称?
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Java/Tomcat 4.1/webapps/ROOT/user/books.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("before connection");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement();
类似这样写的
不过这个是access的写法,有SQL Server的写法
他写在那个地方肯定是!除非他写错了!
你去试一下把。!你那样我没有试过!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement
这样应该是可以实现,问题是能用不能用,在已经写的程序里面
可以这些写吗?我反正看代码是怪怪的,很久没用odbc的方法连接数据库了,这次倒是长见识了
难道越写越退步了?
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
这样肯定是可以!但是还要那个microsoft所提供的包!
包已经带在里面了,应该没什么问题
jdk 1.42
tomcat 4.2
我不知道程序上需要作改动不,这个才是关键问题
你最好把jdk和tomcat版本升上去
jdk 5.0对oracle支持不好,特别慢
这也是我比较郁闷的,要不谁用低版本的,麻烦啊
哈哈!
这么烂??分层吧,容易调试