大家好,又见面了,我是你们的朋友全栈君。
1,准备jdbc的连接jar包,然后在eclipse中创建一个java项目(不是web项目)。
2,将jdbc的jar包build path 或者copy到java项目中都可以,等下给出项目结构图。
3,给出 jdbcc类的连接代码,
package jdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class Jdbcc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName(“com.mysql.jdbc.Driver”);
//创建一个连接
Connection conn=
DriverManager.getConnection(“jdbc:mysql://localhost:3306/first_test”,”root”,”123″);
//使用DriverManager的getConnectin(String url , String username ,
//String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和
//密码来获得。
//创建一个sql 语句支持对象。
Statement stm = (Statement) conn.createStatement();
//执行查询语句
ResultSet rs=(ResultSet)stm.executeQuery(“select * from score”);
//输出结果
while(rs.next())
{
System.out.print(rs.getInt(“id”) ”:” rs.getString(“name”) ”:” rs.getString(“address”));
}
//关闭连接
rs.close();
stm.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4,结构图,通过yog可以查看我们的first_test数据库和它下面的表数据结构,然后在eclipse中能够执行简单查询语句,查询出数据。
5, 如果出现一警告说明 :
Thu Jul 27 09:12:34 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45 , 5.6.26 and 5.7.6 requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
就是使用JDBC跟你的数据库连接的时候,你的JDBC版本与MySQL版本不兼容,MySQL的版本更高一些,在连接语句后加上“useSSL=‘true’” ,就可以连接到数据库了。更高版本.
如下
?characterEncoding=utf8&useSSL=true
8-31号更新,在servlet中连接数据库
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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.mysql.jdbc.PreparedStatement;
@WebServlet(“/CheckServlet”)
public class CheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String DEVICE=”com.mysql.jdbc.Driver”;
public static final String URL=”jdbc:mysql://localhost:3306/first_test?characterEncoding=utf8&useSSL=true”;
public static final String USERNAME=”root”;
public static final String PASSWORD=”123456″;
public CheckServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append(“Served at: “).append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html”);
Connection conn =null;
PreparedStatement stm = null;
ResultSet resultSet= null;
PrintWriter out = response.getWriter();
String userid=request.getParameter(“userid”); //接收验证userid
try {
Class.forName(DEVICE);
//创建一个连接
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
//使用DriverManager的getConnectin(String url , String username ,
//String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和//密码来获得。
//创建一个sql语句支持对象。
String sql=”SELECT COUNT(userid) FROM new_user WHERE userid=?”;
stm = (PreparedStatement) conn.prepareStatement(sql); //实例化 stm
stm.setString(1, userid); //设置查询参数
//执行查询操作
resultSet = stm.executeQuery();
if (resultSet.next()) {
if (resultSet.getInt(1)>0) {
out.println(“true”); //输出信息
}else {
out.println(“false”);
}
}
//关闭连接
resultSet.close();
stm.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/197497.html原文链接:https://javaforall.cn