今天做作业连了一天的mysql。
最后我痛定思痛,决定从0开始学习jsp,省的又面临不会的局面。
忙活了一晚上,终于把数据库连接上了,不过,好伤心啊,我连个数据库都这么墨迹。。。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="data.database"%> <%@page import="java.sql.*"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <% String server="localhost";//MYSQL 服务器的地址 String dbname="rowanhao";//MYSQL 数据库的名字 String user="root";//MYSQL 数据库的登录用户名 String pass="zp930710";//MYSQL 数据库的登录密码 String port="3306";//SQL Server 服务器的端口号,默认为1433 String url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?user="+user+"&password="+pass+"&useUnicode=true&characterEncoding=GB2312"; Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection conn= DriverManager.getConnection(url); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from student"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { out.println(rs.getString(1)); } rs.close(); out.println("aaa"); %> </body> </html>
附带:mysql中my.ini的配置
[mysqld] # set basedir to installation path, e.g., c:/mysql # 设置为MYSQL的安装目录 basedir=F:/datebase/mysql # set datadir to location of data directory, # e.g., c:/mysql/data or d:/mydata/data # 设置为MYSQL的数据目录 datadir=F:/datebase/mysql/data [WinMySQLadmin] Server=F:/datebase/mysql/mysqld-nt.exe #user=root #password=root #以上是设置WinMySQLadmin的配置 #default-storage-engine=MyISAM