时间:2021-07-01 10:21:17 帮助过:3人阅读
- public class JDBCTest {
- public static void main(String[] args) {
- String sql = "SELECT * FROM usertest";
- Connection conn = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wzz","root","1020");
- st = conn.createStatement();
- rs = st.executeQuery(sql);
- while(rs.next()){
- System.out.println(rs.getString("name")+rs.getInt("age"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- rs.close();
- } catch (Exception e2) {
- }
- try {
- st.close();
- } catch (Exception e3) {
- }
- try {
- conn.close();
- } catch (Exception e4) {
- }
- }
- }
- }<br><br><br><br><br>
- public class JDBCTest {
- public static Connection getConnection() {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wzz","root","1020");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- public static void main(String[] args) {
- Connection conn = getConnection();
- Statement st = null;
- try {
- String sql="insert into usertest(name,age) values(‘abc‘,18)";
- st = conn.createStatement();
- int count = st.executeUpdate(sql);
- System.out.println(count);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- st.close();
- } catch (Exception e1) {
- }
- try {
- conn.close();
- } catch (Exception e2) {
- }
- }
- }
- }
mysql jdbc连接
标签: