不知道idea和eclipse在导入库这一块有什么区别,但是总要尝试一下,结果发现这里和eclipse是一样的
在java Application中使用和在web中使用是不一样的,eclipse中的run as -> java Aplication 和 run on server就是这样
在java中使用jdbc
打开Project Structer
选择Modules -> Dependencies -> Module source -> "+"号 -> JARs or directories
这里我已经添加好了,所以会出现驱动包的名称
找到驱动包所在位置 -> OK
这时驱动包会被添加到Dependencies中,再次点击OK,添加完成
接下来测试一下
新建一个类JdbcTest
package com.tsymq.javatest;
import java.sql.*;
public class JdbcTest {
public static void main(String[] args){
//com.mysql.cj.jdbc.Driver;
String url = "jdbc:mysql://localhost:3306/book?serverTimezone=GMT%2B8";
String driver = "com.mysql.cj.jdbc.Driver";
String user = "root";
String pswd = "root";
Connection con = null;
try{
Class.forName(driver);
}
catch (ClassNotFoundException e){
System.out.println("Driver not Found!");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, user, pswd);
System.out.println("Succeed!");
String query = "select * from book_info;";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
System.out.println(rs.getString(1) + "\t"
+ rs.getString(2) + "\t"
+ rs.getString(3) + "\t"
+ rs.getString(4));
}
rs.close();
con.close();
}
catch (Exception e){
e.printStackTrace();
System.out.println("Failed Connection!");
}
}
}
Nice!搞定
在web中使用jdbc
和eclipse一样,将驱动包放在web->WEB-INF->lib文件夹中即可
注意:eclipse中是WebContent,idea中是web
idea默认没有lib文件夹,所以自己新建一个
输入lib -> OK
然后直接将驱动包复制进来即可
新建一个servlet试一下
JdbcServlet.java
package com.tsymq.webtest;
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 java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
@WebServlet(name = "JdbcServlet", urlPatterns = "/Jdbc")
public class JdbcServlet extends HttpServlet {
private String url = "jdbc:mysql://localhost:3306/book?serverTimezone=GMT%2B8";
private String driver = "com.mysql.cj.jdbc.Driver";
private String user = "root";
private String pswd = "root";
Connection con = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<title>Jdbc测试</title>");
try{
Class.forName(driver);
}
catch (ClassNotFoundException e){
System.out.println("Driver not Found!");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, user, pswd);
System.out.println("Succeed!");
String query = "select * from book_info;";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
out.println("<table border=1 >");
out.println("<tr><th>编号</th><th>书名</th><th>作者</th><th>出版社</th></tr>");
while (rs.next()){
out.println("<tr>");
out.println("<td>" + rs.getString(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
out.println("<td>" + rs.getString(3) + "</td>");
out.println("<td>" + rs.getString(4) + "</td>");
out.println("</tr>");
}
out.println("</table>");
rs.close();
con.close();
}
catch (Exception e){
e.printStackTrace();
System.out.println("Failed Connection!");
}
out.close();
}
}
Nice!搞定!