JDBC連接mysql5.7簡(jiǎn)單例子

1,準(zhǔn)備jdbc的連接jar包,然后在eclipse中創(chuàng)建一個(gè)java項(xiàng)目(不是web項(xiàng)目)。

2,將jdbc的jar包build path 或者copy到j(luò)ava項(xiàng)目中都可以,等下給出項(xiàng)目結(jié)構(gòu)圖。

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");

//創(chuàng)建一個(gè)連接

Connection conn=

DriverManager.getConnection("jdbc:mysql://localhost:3306/first_test","root","123");

//使用DriverManager的getConnectin(String url , String username ,

//String?password?)方法傳入指定的欲連接的數(shù)據(jù)庫(kù)的路徑、數(shù)據(jù)庫(kù)的用戶名和

//密碼來(lái)獲得。

//創(chuàng)建一個(gè)sql 語(yǔ)句支持對(duì)象。

Statement stm = (Statement) conn.createStatement();

//執(zhí)行查詢語(yǔ)句

ResultSet? rs=(ResultSet)stm.executeQuery("select * from score");

//輸出結(jié)果

while(rs.next())

{

System.out.print(rs.getInt("id")+":"+ rs.getString("name")+":"+rs.getString("address"));

}

//關(guān)閉連接

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,結(jié)構(gòu)圖,通過(guò)yog可以查看我們的first_test數(shù)據(jù)庫(kù)和它下面的表數(shù)據(jù)結(jié)構(gòu),然后在eclipse中能夠執(zhí)行簡(jiǎn)單查詢語(yǔ)句,查詢出數(shù)據(jù)。


5, 如果出現(xiàn)一警告說(shuō)明 :

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跟你的數(shù)據(jù)庫(kù)連接的時(shí)候,你的JDBC版本與MySQL版本不兼容,MySQL的版本更高一些,在連接語(yǔ)句后加上“useSSL=‘true’” ,就可以連接到數(shù)據(jù)庫(kù)了。更高版本.

如下

?characterEncoding=utf8&useSSL=true



8-31號(hào)更新,在servlet中連接數(shù)據(jù)庫(kù)

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"); //接收驗(yàn)證userid

try {

Class.forName(DEVICE);

//創(chuàng)建一個(gè)連接

conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);

//使用DriverManager的getConnectin(String url , String username ,

//String password )方法傳入指定的欲連接的數(shù)據(jù)庫(kù)的路徑、數(shù)據(jù)庫(kù)的用戶名和//密碼來(lái)獲得。

//創(chuàng)建一個(gè)sql語(yǔ)句支持對(duì)象。

String sql="SELECT COUNT(userid) FROM new_user WHERE userid=?";

stm = (PreparedStatement) conn.prepareStatement(sql);? //實(shí)例化 stm

stm.setString(1, userid); //設(shè)置查詢參數(shù)

//執(zhí)行查詢操作

resultSet = stm.executeQuery();

if (resultSet.next()) {

if (resultSet.getInt(1)>0) {

out.println("true");? //輸出信息

}else {

out.println("false");

}

}

//關(guān)閉連接

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();

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容