JDBC连接MYSQL实例

  • 格式:doc
  • 大小:157.50 KB
  • 文档页数:10

JDBC连接MYSQL实例 1.MyEclipse内部的Tomcat一直配置不好,所以直接用内置的Tomcat访问失败,还是选择了放在C盘的外置Tomcat 7.* 和 D盘虚拟目录Tomcat 联合测试网页 为了以后测试方便,可以重新设置虚拟目录,和MyEclipse的workspace。 比如说workspace设为原来D:\Tomcat里,这样,先复制一下原来workspace文件夹里的.metadata文件夹,因为workspace需要这个,不然启动时要加载很长时间。 然后修改Tomcat的虚拟目录,在 C:\apache-tomcat-7.0.26\conf 里的server.xml里,上次我们把8080改成了80,/host前增加了虚拟目录,现在我们只需要改虚拟目录地址:

重启Tomcat,在bin文件夹里的startup.bat,现在,MyEclipse

里调的jsp可以直接用Tomcat 7.* 访问了,如输入: http://localhost/demo/ 或: http://10.7.16.4/demo/ 2.executeQuery()方法

<%@ page language="java" import="java.util.*"
pageEncoding="gb2312"%> <%@ page import="java.sql.*" %> <%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.get
ServerPort()+path+"/";
%>

My JSP 'test.jsp' starting page

content="keyword1,keyword2,keyword3">

<%
String dbName="firstjsp";
String tableName="users";
String url="jdbc:mysql://localhost/"+dbName; //打开dbName数据库
String userName="root";
String password="lfw123";
String sql=null;
Connection conn=null;
Statement stmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.println("加载驱动器类时出现异常");
}
try{
conn=DriverManager.getConnection(url,userName,password);
//创建Statement语句
stmt=conn.createStatement();
sql="SELECT * FROM "+tableName;

//使用executeQuery执行SQL查询语句
ResultSet rs=stmt.executeQuery(sql);
%> 编号 姓名 密码 <%
while(rs.next()){
int id=rs.getInt(1);
int name=rs.getInt(2);
String pwd=rs.getString(3);
%> <%=id %> <%=name %> <%=pwd %> <%
}
rs.close();
stmt.close();
}catch(SQLException e){
out.println("出现SQL异常");
}finally{
//关闭语句和数据库连接
try{
if(conn!=null) conn.close();
}catch(SQLException e){
out.println("关闭数据库连接时出现异常");
}
}
%>

测试结果: (long1 vs longpo 转载请注明出处:http://blog.163.com/lfw2565295@126) 3.executeUpdate()方法 executeUpdate()更新数据库,INSERT, UPDATE, DELETE语句,返回值是一个整数,表示受影响的行数(更新计数), 对于CREATE TABLE, DROP TABLE等不操作行的语句,返回宗师0。 <%@ page language="java" import="java.util.*"
pageEncoding="gb2312"%> <%@ page import="java.sql.*" %> <%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.get
ServerPort()+path+"/";
%>

My JSP 'test.jsp' starting page

content="keyword1,keyword2,keyword3">

<%
String dbName="firstjsp";
String tableName="users";
String url="jdbc:mysql://localhost/"+dbName; //打开dbName数据库
String userName="root";
String password="lfw123";
String sql=null;
Connection conn=null;
Statement stmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.println("加载驱动器类时出现异常");
}
try{
conn=DriverManager.getConnection(url,userName,password);
//创建Statement语句
stmt=conn.createStatement();

int affectedRowCount;
//使用executeUpdate执行SQL更新语句

sql="DELETE FROM "+tableName+" WHERE userName=234 ";
affectedRowCount=stmt.executeUpdate(sql);
out.println("删除操作影响的数据行数为:" + affectedRowCount
+"
");

sql="update "+tableName+" set passwd='caobb' where userId=2 ";
affectedRowCount=stmt.executeUpdate(sql);
out.println("修改操作影响的数据行数为:" + affectedRowCount
+"
");

sql="INSERT INTO "+tableName+"(userName,passwd)
VALUES('234','icq')";
affectedRowCount=stmt.executeUpdate(sql);
out.println("插入操作影响的数据行数为:" + affectedRowCount
+"
");

sql="SELECT * FROM "+tableName;
ResultSet rs=stmt.executeQuery(sql);
%>