如何用JSP连接安装在Linux上的MySQL

如何用JSP连接安装在Linux上的MySQL,第1张

第一. linux 服务器mysql端口要是通的.保证能跟windows ping 通.

第二. 新建个用户.把主机选成任意主机. 或者直接把root的host修改成%

第三.确保你jsp连接字符串是正确 的.

在eclipse里新建工程,然后把mysql-connector-java-5.1.5.jar包拷贝到Webroot>WEB-INF>lib目录下。

然后新建一个类,(也可以直接在jsp页面写)

package com

import java.sql.*

public class ConnMysql {

public ConnMysql() {

super()

}

private static ConnMysql instance = null

//获得连接

public static synchronized Connection my_getConnection(){

if (instance == null) {

instance = new ConnMysql()

}

return instance.me_getConnection()

}

//获得连接所需要的属性数据等值

private Connection me_getConnection(){

try {

Class.forName("com.mysql.jdbc.Driver").newInstance() String url ="jdbc:mysql://localhost:3306/数据库名?user=用户名&password=密码&useUnicode=true&characterEncoding=GB2312"

return DriverManager.getConnection(url)

} catch (Exception ex) {

System.out.print(ex.getMessage())

return null

}

}

}

在jsp页面中

<%@page import="java.sql.*" %>

<%@page import="com.ConnMysql" %>

<%

Connection con = null

Statement s = null

ResultSet rs = null

con =ConnMysql.my_getConnection()

try{

s = con.createStatement()

}catch(Exception e){

System.out.print("statement error!")

}

%>

<% //

rs = s.executeQuery("SELECT * FROM ...")

%>

eclipse,我用的是eclipse,也可以用其他的开发工具。

tomcat,tomcat是用来做服务器的,如果eclipse还没有配置tomcat服务器,就要先配置好tomcat服务器。

MySQL,本文用的是MySQL数据库,读者也可以尝试用其他的数据库,但是连接驱动就要换成相应数据库的连接驱动。

连接驱动 mysql-connector-java-3.1.14-bin.jar,可以从网上下载。

方法/步骤

打开eclipse,菜单栏下,File-new,打开Dynamic Web Project,创建一个jsp project,为方便起见,本文直接在jsp页面里写java代码进行数据库的连接。。大部分网友应该都可以看懂这段代码的涵义,这里就不赘述了。

其中需要注意的是  String url="jdbc:mysql://localhost:3306/mydb"  其中的3306是MySQL安装时的端口号,默认的是3306,如果你安装MySQL时更改了端口号就要在这里填写你更改的端口号。下面是我的jsp文档

<%@ page language="java" contentType="text/htmlcharset=UTF-8"

pageEncoding="UTF-8"%>

<%@page import="java.sql.Connection" %>

<%@page import="java.sql.*" %>

<%@page import="java.sql.DriverManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<table border="1" align="center">

<tr>

<td>书名</td>

<td>作者</td>

</tr>

<%

String driverClass="com.mysql.jdbc.Driver"

String url="jdbc:mysql://localhost:3306/mydb"

String user="root"

String password="1234"

String a="zhangsan"

Connection conn

try{

Class.forName(driverClass)

conn=DriverManager.getConnection(url,user,password)

Statement stmt=conn.createStatement()

String sql="select * from books"

ResultSet rs=stmt.executeQuery(sql)

while(rs.next()){

%>

<tr>

<td><%=rs.getString("bookname") %></td>

<td><%=rs.getString("writer") %></td>

</tr>

<%

}

}

catch(Exception ex){

ex.printStackTrace()

}

%>

</table>

</body>

</html>

然后在这个project的WebContent\WEB-INF\lib的文件夹里添加之前提到的连接驱动 mysql-connector-java-3.1.14-bin.jar,将其复制到lib的文件夹中。保存项目,然后运行,就会在网页中出现表格。

3

是不是很简单呢,希望这篇经验能够给大家带来方便。

END

注意事项

要确保在MySQL中存在mydb数据库,mydb中已创建books表格,不然会提示错误的。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5908674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-08
下一篇 2023-03-08

发表评论

登录后才能评论

评论列表(0条)

保存