import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet;
讯享网
public class Test {
讯享网</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){ </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">首先加载驱动</span> Class.forName("com.mysql.cj.jdbc.Driver"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提供JDBC连接的URL</span> String url="jdbc:mysql://<strong><span style="color: rgba(255, 0, 0, 1)">0.0.0.0</span></strong>:3306/<strong><span style="color: rgba(255, 0, 0, 1)">xxxx</span></strong>"<span style="color: rgba(0, 0, 0, 1)">; String username</span>="root"<span style="color: rgba(0, 0, 0, 1)">; String password</span>="root"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建数据库的连接</span> Connection con =<span style="color: rgba(0, 0, 0, 1)"> DriverManager.getConnection(url,username,password); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个statement执行者</span> String sql="SELECT * FROM biz_spot WHERE spot_id = ?"<span style="color: rgba(0, 0, 0, 1)">; PreparedStatement statement </span>=<span style="color: rgba(0, 0, 0, 1)"> con.prepareStatement(sql); statement.setLong(</span>1,11L<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行SQL语句</span> ResultSet result =<span style="color: rgba(0, 0, 0, 1)"> statement.executeQuery(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理返回结果</span> <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (result.next()){ System.out.println(result.getString(</span>"<strong><span style="color: rgba(255, 0, 0, 1)">xxx</span></strong>") + "---" + result.getString("<strong><span style="color: rgba(255, 0, 0, 1)">xxx</span></strong>"<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭JDBC对象</span> con.close();
result.close(); statement.close(); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(ClassNotFoundException e){ System.out.println(</span>"加载驱动失败!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){ } } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/140357.html