C:\Program Files\MySQL\Connector J 5.1.25\mysql-connector-java-5.1.25-bin.jar
(这是我的路径,记得用你自己的路径)
JAVA测试代码
import java.util.*; import java.sql.*; public class Tsql { public static void main(String args[]) throws Exception { Class.forName("com.mysql.jdbc.Driver");//驱动 String url="jdbc:mysql://localhost/lxy?user=root&password=123456";//链 接的数据库,用户名,密码 Connection con = DriverManager.getConnection(url); String sql = "select * from tbl_student"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { System.out.println("姓名:"+rs.getString("sname")); } //记得关闭资源 rs.close(); pstmt.close(); con.close(); } }
<!-- 链接MYSQL数据库, --> <%@ page contentType="text/html;charset=GB2312"%> <%@ page import="java.sql.*"%> <html> <head> <title>JSP测试链接MYSQL</title> </head> <body> <% Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/lxy?user=root&password=123456"; Connection con = DriverManager.getConnection(url); String sql = "select * from tbl_student"; PreparedStatement pstmt =con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { out.println("姓名:"+rs.getString("sname")+",性别:"+rs.getString("ssex")+"<br />"); } rs.close(); pstmt.close(); con.close(); %> </body> </html>