java连接MySql数据库
程序员文章站
2024-03-13 17:57:39
...
先给项目:
链接:https://pan.baidu.com/s/1ddbhL4TiWLVPWcr4nK4Epg 密码:alao
准备mysql,如果没有安装mysql,下面提供我写的如何安装mysql(资源在百度云,免费的)
https://blog.csdn.net/qq_36325069/article/details/79877050
在给个mysql的jdbc包 链接:https://pan.baidu.com/s/1wXotLZcgd6zFZZbNEvOCrQ 密码:aafa
环境:jdk配置好了,eclipse准备好了,eclipse配置好Tomcat了,可以去百度,都很好弄
给出几个链接
eclipse:3个版本,,win64位
链接:https://pan.baidu.com/s/1FwmoQ3wjKwKqxu8y6B-jrg 密码:z1cd
jdk:两个,1.6和1.8 win64位
链接:https://pan.baidu.com/s/1KBALvq-XuLDj-Iaj7EFpLA 密码:t1vx
Tomcat:3个
链接:https://pan.baidu.com/s/1XJbwryQUNPiq44wCChxECg 密码:80no
准备工作做好了,接下来就是今天的主题了
- 新建javaweb项目,添加jar,并添加到path中,目录如下
JDBC.java文件
package com.quding.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class JDBC {
//加载数据库驱动
private static String dbdriver ="com.mysql.jdbc.Driver";
//获取mysql链接地址,其中sh为数据库名称
private static String dburl = "jdbc:mysql://127.0.01:3306/sh?characterEncoding=utf8&useSSL=true";
//用户名称
private static String username = "root";
//密码
private static String password = "newpassword";
//数据库链接
private static Connection conn = null;
/**
* 获取数据库链接
* @return
*/
private static void initConnction() {
while(conn == null) {
doConnection();
}
}
private static void destoryConnection() {
if(conn != null) {
try {
conn.close();
System.out.println("链接关闭成功。。。。。。true");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("链接关闭失败。。。。。。false");
}
}
}
/**
* 进行数据库链接
*/
private static void doConnection() {
// TODO Auto-generated method stub
try {
//通过驱动名称加载驱动
Class.forName(dbdriver);
System.out.println("加载数据库驱动。。。。。true");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("加载数据库驱动。。。。。false");
}
try {
Connection connection = DriverManager.getConnection(dburl, username, password);
conn = connection;
System.out.println("获取链接成功。。。。。。。true");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取链接失败。。。。。。。false");
}
}
/**
* 查询方法,public,对外提供
*/
public static List<List<Object>> query(String sql){
//
initConnction();
List<List<Object>> list = new ArrayList<List<Object>>();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps= conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
List<Object> lst = new ArrayList<Object>();
for (int i = 1; i <= columnCount; ++i) {
lst.add(rs.getObject(i) == null ? "" : rs.getObject(i));
}
list.add(lst);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//closeAll(rs, ps);
}
return list;
}
/**
* 关闭链接
* @param rs2
* @param ps2
*/
public static void closeAll(ResultSet rs2, PreparedStatement ps2) {
// TODO Auto-generated method stub
destoryConnection();
try {
ps2.cancel();
rs2.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.quding.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.quding.mysql.JDBC;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//f防止中文乱码
response.setContentType("text/html;charset=UTF-8;pageEncoding=UTF-8");
String sql = "select * from book where 1=1";
List<List<Object>> list = JDBC.query(sql);
String ss = list.get(0).toString();
response.getWriter().append(ss);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
上面是TestServlet.java文件
运行
结果
我的数据库
特别提醒:这种方式写一个小的服务还是可以的,先熟悉这样的写法,然后建议用hibernate或Mybatis框架,可以去了解一下,开发会省事很多,找工作也好找一些(比如我,嘿嘿)