1 数据库
mysql> select * from users;
+----+------+----------+
| id | name | password |
+----+------+----------+
| 1 | tom | 123 |
+----+------+----------+
1 row in set (0.07 sec)
2 maven依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
3 jdbc连接案例
package net920vip;
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
stmt = conn.createStatement();
String sql = "select * from users";
rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
System.out.println("id: " + id + ",name: " + name + ",password: " + password);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
输出:
id: 1,name: tom,password: 123
4 指定用户名查询
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "root");
String sql = "select * from users where username like ? and id = ?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, "%root%");
pstm.setInt(2, 1);
rs = pstm.executeQuery();
if (rs.next()) {
int id = rs.getInt(1);
String username = rs.getString(2);
System.out.println("id:" + id + ", username:" + username);
}
<h2><a id="1__0"></a>1 数据库</h2>
<ul>
<li>
<p>数据库:student</p>
</li>
<li>
<p>表:users</p>
</li>
</ul>
<pre><div class="hljs"><code class="lang-shell"><span class="hljs-meta">mysql> </span><span class="language-bash">select * from <span class="hljs-built_in">users</span>;</span>
+----+------+----------+
| id | name | password |
+----+------+----------+
| 1 | tom | 123 |
+----+------+----------+
1 row in set (0.07 sec)
</code></div></pre>
<h2><a id="2_maven_14"></a>2 maven依赖</h2>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-tag"><<span class="hljs-name">dependencies</span>></span>
<span class="hljs-comment"><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>mysql<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>mysql-connector-java<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>5.1.8<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependencies</span>></span>
</code></div></pre>
<h2><a id="3_jdbc_27"></a>3 jdbc连接案例</h2>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> net920vip;
<span class="hljs-keyword">import</span> java.sql.*;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JDBCTest</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-type">Connection</span> <span class="hljs-variable">conn</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;
<span class="hljs-type">Statement</span> <span class="hljs-variable">stmt</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;
<span class="hljs-type">ResultSet</span> <span class="hljs-variable">rs</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;
<span class="hljs-keyword">try</span> {
Class.forName(<span class="hljs-string">"com.mysql.jdbc.Driver"</span>);
<span class="hljs-comment">// String url = "jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8";</span>
conn = DriverManager.getConnection(<span class="hljs-string">"jdbc:mysql://localhost:3306/student"</span>, <span class="hljs-string">"root"</span>, <span class="hljs-string">"root"</span>);
stmt = conn.createStatement();
<span class="hljs-type">String</span> <span class="hljs-variable">sql</span> <span class="hljs-operator">=</span> <span class="hljs-string">"select * from users"</span>;
rs = stmt.executeQuery(sql);
<span class="hljs-keyword">while</span> (rs.next()) {
<span class="hljs-type">int</span> <span class="hljs-variable">id</span> <span class="hljs-operator">=</span> rs.getInt(<span class="hljs-number">1</span>);
<span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> rs.getString(<span class="hljs-number">2</span>);
<span class="hljs-type">String</span> <span class="hljs-variable">password</span> <span class="hljs-operator">=</span> rs.getString(<span class="hljs-number">3</span>);
System.out.println(<span class="hljs-string">"id: "</span> + id + <span class="hljs-string">",name: "</span> + name + <span class="hljs-string">",password: "</span> + password);
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">if</span> (rs != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">try</span> {
rs.close();
} <span class="hljs-keyword">catch</span> (SQLException throwables) {
throwables.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (stmt != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">try</span> {
stmt.close();
} <span class="hljs-keyword">catch</span> (SQLException throwables) {
throwables.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (conn != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">try</span> {
conn.close();
} <span class="hljs-keyword">catch</span> (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
</code></div></pre>
<p>输出:</p>
<pre><code class="lang-">id: 1,name: tom,password: 123
</code></pre>
<h2><a id="4__88"></a>4 指定用户名查询</h2>
<pre><div class="hljs"><code class="lang-java">Class.forName(<span class="hljs-string">"com.mysql.jdbc.Driver"</span>);
<span class="hljs-comment">// String url = "jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8";</span>
conn = DriverManager.getConnection(<span class="hljs-string">"jdbc:mysql://localhost:3306/jdbc"</span>, <span class="hljs-string">"root"</span>, <span class="hljs-string">"root"</span>);
<span class="hljs-type">String</span> <span class="hljs-variable">sql</span> <span class="hljs-operator">=</span> <span class="hljs-string">"select * from users where username like ? and id = ?"</span>;
pstm = conn.prepareStatement(sql);
pstm.setString(<span class="hljs-number">1</span>, <span class="hljs-string">"%root%"</span>);
pstm.setInt(<span class="hljs-number">2</span>, <span class="hljs-number">1</span>);
rs = pstm.executeQuery();
<span class="hljs-keyword">if</span> (rs.next()) {
<span class="hljs-type">int</span> <span class="hljs-variable">id</span> <span class="hljs-operator">=</span> rs.getInt(<span class="hljs-number">1</span>);
<span class="hljs-type">String</span> <span class="hljs-variable">username</span> <span class="hljs-operator">=</span> rs.getString(<span class="hljs-number">2</span>);
System.out.println(<span class="hljs-string">"id:"</span> + id + <span class="hljs-string">", username:"</span> + username);
}
</code></div></pre>
留言