jsp java jdbc_jsp 访问jdbc实例

前提条件:

1.安装Java,tomcat,MySQL,eclipse

2.配置java 环境变量

3.配置MySQL参数

4.tomcat能正常启动

使用eclipse编写代码:

1. 新建一个web project. 选择“Dynamic Web Project”. 按提示往下继续进行。

W0Js7WAAAAAElFTkSuQmCC

2. 工程建立完成后,在Webcontent目录下,右键new->jsp file

3. 输入jsp代码如下:

pageEncoding="ISO-8859-1"%>

Test mysql connectivity

java.sql.Connection conn;

java.lang.String strConn;

conn=null;

//Connect to the database

Class.forName("com.mysql.jdbc.Driver").newInstance();

try

{

conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema?useSSL=false","root","wen_201611!");

} catch (SQLException e)

{

System.out.println(e.getMessage());

}

//Connnect success, then query the data from table1, and display the content in page

if (conn!=null)

{

out.println("database connect success");

Statement stmt=null;

ResultSet rs=null;

try {

stmt=conn.createStatement();

rs=stmt.executeQuery("SELECT * FROM table1");

while (rs.next())

{

%>

The value of row col 1 is

The value of row col 2 is

}

} catch (SQLException ex)

{

System.out.println("SQLException: "+ex.getMessage());

System.out.println("SQLState: "+ex.getSQLState());

System.out.println("VendorError: "+ex.getErrorCode());

}

//release resources

finally

{

if (rs!=null)

{

try{

rs.close();

System.out.println("\nrs destoryed");

} catch (SQLException sqlEx) {}

rs=null;

}

if (stmt!=null)

{

try{

stmt.close();

System.out.println("stmt destoryed");

} catch (SQLException sqlEx){}

stmt=null;

}

}

}

else

out.println("database connect failed");

%>

4. 点击eclipse的运行按钮,可以运行该jsp页面。

注意事项:

遇到无法找到jdbc类的异常的话,将jdbc connecter 拷贝到tomcat安装目录下的lib目录下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是一个最简单,也是学习jsp必须会的一个入门实验----通过jdbc连接数据库。 采用jsp+access数据库实现,同时使用javabean通过jdbc连接数据库。运行结果将显示从数据库查询出的图书。里面包含了javabean的源代码及解释。 提示:如果是要下jsp图书管理系统的朋友,请下载我上传的另一个资源----jsp+access图书管理系统。就不要再下本资源了,本资源只是做了一个图书的表,用以连接数据库查询图书,并没有对图书的增、删、改功能。 javabean源代码: package sjob; import java.sql.*; public class connBook { private Statement state=null; private Connection conn = null; ResultSet results=null; public connBook() { } public void openConn()throws Exception //连接数据库 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:book"; conn=DriverManager.getConnection(url,"",""); //数据库、用户名、密码 ,创建连接对象 } catch(SQLException e) //异常处理 { System.err.println("Data.executeQuery: " +e.getMessage()); } } public ResultSet queryResult(String query) { results = null; try { state=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //创建访问数据库的对象 results=state.executeQuery(query); //查询并返回结果 } catch(SQLException e) { System.err.println("Data.executeQuery: " + e.getMessage()); } return results; } public void closeState() { try { state.close(); //关闭访问数据库的对象 } catch(SQLException e) { System.err.println("Date.executeQuery: " + e.getMessage()); } } public void closeConn() { try { conn.close(); //断开连接 } catch(SQLException e) { System.err.println("Data.executeQuery: " + e.getMessage()); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值