时间: 2021-07-31 作者:daque
此刻有许多入门jsp的网友常常会问数据库如何贯穿啊,如何老堕落啊?以是我会合的在这写篇作品供大师参考,本来这种把数据库论理十足放在jsp里偶然是好的做法,然而利于于入门者进修,以是我就如许做了,当大师学好确定水平的功夫,不妨商量用mvc的形式开拓。在熟习那些代码的功夫,你确定将jdbc的启动步调放到效劳器的类路途里,而后要在数据库里建一个表test,有两个字段比方为test1,test2,不妨用底下sql建 create table test(test1 varchar(20),test2 varchar(20) 而后向这个表写入一条尝试记录 那么此刻发端咱们的jsp和数据库之旅吧。 一、jsp贯穿oracle8/8i/9i数据库(用thin形式) testoracle.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("oracle.jdbc.driver.oracledriver").newinstance(); string url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为你的数据库的sid string user="scott"; string password="tiger"; connection conn= drivermanager.getconnection(url,user,password); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 二、jsp贯穿sql server7.0/2000数据库 testsqlserver.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%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(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 三、jsp贯穿db2数据库 testdb2.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("com.ibm.db2.jdbc.app.db2driver ").newinstance(); string url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 string user="admin"; string password=""; connection conn= drivermanager.getconnection(url,user,password); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 四、jsp贯穿informix数据库 testinformix.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("com.informix.jdbc.ifxdriver").newinstance(); string url = "jdbc:informix-sqli://123.45.67.89:1533/testdb:informixserver=myserver; user=testuser;password=testpassword"; //testdb为你的数据库名 connection conn= drivermanager.getconnection(url); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 五、jsp贯穿sybase数据库 testmysql.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("com.sybase.jdbc.sybdriver").newinstance(); string url =" jdbc:sybase:tds:localhost:5007/tsdata"; //tsdata为你的数据库名 properties sysprops = system.getproperties(); sysprops.put("user","userid"); sysprops.put("password","user_password"); connection conn= drivermanager.getconnection(url, sysprops); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 六、jsp贯穿mysql数据库 testmysql.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("org.gjt.mm.mysql.driver").newinstance(); string url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true&characterencoding=8859_1" //testdb为你的数据库名 connection conn= drivermanager.getconnection(url); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html> 七、jsp贯穿postgresql数据库 testmysql.jsp如次: <%@ page contenttype="text/html;charset=utf8"%> <%@ page import="java.sql.*"%> <html> <body> <%class.forname("org.postgresql.driver").newinstance(); string url ="jdbc:postgresql://localhost/soft" //soft为你的数据库名 string user="myuser"; string password="mypassword"; connection conn= drivermanager.getconnection(url,user,password); statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable); string sql="select * from test"; resultset rs=stmt.executequery(sql); while(rs.next()) {%> 您的第一个字段实质为:<%=rs.getstring(1)%> 您的第二个字段实质为:<%=rs.getstring(2)%> <%}%> <%out.print("数据库操纵胜利,祝贺你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html>