Java连接示例
<p>Java连接示例如下:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">import java.sql.*;</span></p>
<p><span style="font-family:Consolas">import com.mysql.jdbc.Driver;</span></p>
<p><span style="font-family:Consolas">public class Demo {</span></p>
<p><span style="font-family:Consolas"> public static void main(String[] args) throws SQLException, ClassNotFoundException{</span></p>
<p><span style="font-family:Consolas"> String url = "jdbc:mysql://<strong><</strong><strong>域名</strong><strong>></strong>:<strong><</strong><strong>端口</strong><strong>></strong>/<strong><Database></strong>";</span></p>
<p><span style="font-family:Consolas">String user = "<username>";<br />
String pwd= "<password>";</span></p>
<p><span style="font-family:Consolas"> String driverName ="com.mysql.jdbc.Driver";</span></p>
<p><span style="font-family:Consolas"> Connection conn = null;</span></p>
<p><span style="font-family:Consolas"> String sql = "SELECT * FROM t";</span></p>
<p><span style="font-family:Consolas"> try{</span></p>
<p><span style="font-family:Consolas"> //加载驱动</span></p>
<p><span style="font-family:Consolas"> Driver.class.forName(driverName);</span></p>
<p><span style="font-family:Consolas"> //建立连接</span></p>
<p><span style="font-family:Consolas"> conn = DriverManager.getConnection(URL,username,password);</span></p>
<p><span style="font-family:Consolas"> //创建statement执行SQL语句</span></p>
<p><span style="font-family:Consolas"> Statement ps = conn.createStatement();</span></p>
<p><span style="font-family:Consolas"> //结果集处理</span></p>
<p><span style="font-family:Consolas"> ResultSet rs = ps.executeQuery(sql);</span></p>
<p><span style="font-family:Consolas"> while(rs.next()) {</span></p>
<p><span style="font-family:Consolas"> System.out.println(rs.getString("id"));</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> catch (ClassNotFoundException e) {</span></p>
<p><span style="font-family:Consolas"> e.printStackTrace();</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> catch(SQLException e){</span></p>
<p><span style="font-family:Consolas"> e.printStackTrace();</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> //关闭连接</span></p>
<p><span style="font-family:Consolas"> finally{</span></p>
<p><span style="font-family:Consolas"> if(conn!=null){</span></p>
<p><span style="font-family:Consolas"> try{</span></p>
<p><span style="font-family:Consolas"> conn.close();</span></p>
<p><span style="font-family:Consolas"> }catch(SQLException e){</span></p>
<p><span style="font-family:Consolas"> e.printStackTrace();</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas">}</span></p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!