JDBC连接MYSQL

类别:Java 点击:0 评论:0 推荐:

安装JDBC驱动MySQL Connector/J 3.1.6

下载:http://dev.mysql.com/downloads/index.html

解压后将其中的mysql-connector-java-3.1.6-bin.jar复制到D:\Tomcat 5.5\common\lib或者D:\Java\jdk1.5.0_01\jre\lib\ext或者其他地方(则需要设置classpath指向该路径)

测试代码如下:

<%@ page contentType="text/html;charset=GB2312"%>
<%@ page language="java" import="java.sql.*"%>
<%
String mySqlDriver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/test";//数据库连接
 Connection conn;
 Class.forName(mySqlDriver);
 conn = DriverManager.getConnection( url,"root","cleversoft");//用户名与密码
 if(conn==null){
System.out.println("get Conn Error");
}
Statement stmt=conn.createStatement();
ResultSet rs=null;
rs=stmt.executeQuery("select * from newtable");
while(rs.next())
{//循环显示记录
%>
id:<%=rs.getString("id")%> name1:<%=rs.getString("name1")%> name2:(<%=rs.getString("name2")%>)
<%="<br>"%>
<%
}
rs.close();
stmt.close();
conn.close();
%>

本文地址:http://com.8s8s.com/it/it11353.htm