📄 670716.xml
字号:
<%out.print("数据库操作成功,恭喜你");%>   
<%rs.close();   
stmt.close();   
conn.close();   
%>   
</body>   
</html> 
六、jsp连接MySQL数据库 
testmysql.jsp如下: 
<%@ page contentType="text/html;charset=gb2312"%>   
<%@ 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=gb2312"%>   
<%@ 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> 
</Content>
<PostDateTime>2002-4-23 20:42:22</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>晓彬</PostUserNickName>
<rank>两星(中级)</rank>
<ranknum>star2</ranknum>
<credit>135</credit>
<ReplyID>4407348</ReplyID>
<TopicID>670716</TopicID>
<PostUserId>173450</PostUserId>
<PostUserName>Andrawu</PostUserName>
<Point>20</Point>
<Content>一般是通过读取配置文件来连接数据库的。给一个列子吧,连接oracle数据库的,希望对你有帮助。
import java.util.*;
import java.io.*;
public class BaseBean {
	protected DBConn oDBConn = null;
	protected Hashtable oDsConfig = null;
        public BaseBean() throws IOException,FileNotFoundException {
		 try {
           	oDsConfig = new Hashtable();
           	String propertiesFile = "config";
  			String prefix = "com";    //指示资源绑定文件
			PropertyResourceBundle resources=(PropertyResourceBundle)PropertyResourceBundle.getBundle(propertiesFile);
			String ip = (String)resources.getString(prefix + ".ip");
			String username = (String)resources.getString(prefix + ".username");
			String driver = (String)resources.getString(prefix + ".driver");
			String password = (String)resources.getString(prefix + ".password");
			String dbtype = (String)resources.getString(prefix + ".dbtype");
			String sid = (String)resources.getString(prefix + ".sid");
			oDsConfig.put("ip",ip);
			oDsConfig.put("username",username);
			oDsConfig.put("driver",driver);  
			oDsConfig.put("password",password);
			oDsConfig.put("dbtype",dbtype);
			oDsConfig.put("sid",sid);
			oDBConn = new DBConn(oDsConfig)	;   //创建连接实例
		}catch(Exception ex) {
			System.out.println("读数据库配置文件失败!!!" + ex.getMessage());
		}
	}
}
import java.sql.*;
import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public  class DBConn {
    private Connection oDbConnection = null;
    private DataSource oDatasource   = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private CallableStatement cstmt = null; //执行存储过程
    private PreparedStatement pstmt = null; //预处理段
    private Hashtable oDsConfig = null;   //数据库配置
    public DBConn(Hashtable t_DsConfig) throws SQLException {
    	try {
	    	oDsConfig = t_DsConfig ;
	    	String driver = "";
	        driver = (String)oDsConfig.get("driver");
			Class.forName(driver).newInstance();//加载数据库驱动程序
        }catch(Exception ne) {
            System.out.println("加载数据库驱动程序失败!" + ne.getMessage());
        }
    }
    //创建连接
    public void getConnection() {
        try {
            String sid = (String)oDsConfig.get("sid");    
            String ip = (String)oDsConfig.get("ip");      
			String username = (String)oDsConfig.get("username");     
			String password = (String)oDsConfig.get("password");     
			String dbtype = (String)oDsConfig.get("dbtype");	     
            oDbConnection = DriverManager.getConnection(dbtype + ":" + "@" + ip + ":" + "1521" + ":" + sid , username , password);
		}catch(Exception se) {
            System.out.println("创建连接失败! " + se.getMessage());
        }
        return;
    }
}
</Content>
<PostDateTime>2002-4-23 20:42:24</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>见到PLMM就脸红</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>100</credit>
<ReplyID>4416304</ReplyID>
<TopicID>670716</TopicID>
<PostUserId>150822</PostUserId>
<PostUserName>waterdragonfly</PostUserName>
<Point>0</Point>
<Content>收藏先</Content>
<PostDateTime>2002-4-24 12:42:24</PostDateTime>
</Reply>
</Replys>
</Topic>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -