Java Mysql 初级操作

来源:互联网 发布:php 表示根目录 编辑:程序博客网 时间:2024/06/07 05:58

JAVA MySQL 插入 查询

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Set;//import java.util.*; public class maino {        public static void main(String[] args) {                String driver ="com.mysql.jdbc.Driver";                                String url ="jdbc:mysql://localhost:3306/fei";                                String user ="root";                                String password ="";                              try {Class.forName(driver);Connection conn = DriverManager.getConnection(url, user, password);if(!conn.isClosed())System.out.println("Succeeded connecting to the Database!");Statement statement = conn.createStatement();String sql ="select * from info";String sql1 = "INSERT INTO info VALUES ('Bziill', '93rd', '17')";statement.executeUpdate(sql1);ResultSet rs = statement.executeQuery(sql);System.out.println("----------------");System.out.println("Result is :");String name=null;while (rs.next()) {System.out.println(rs.getString("Name")+": "+rs.getString("Age"));}} catch(ClassNotFoundException e) {   System.out.println("Sorry,cant find the Driver!");   e.printStackTrace();   } catch(SQLException e) {   e.printStackTrace();   } catch(Exception e) {   e.printStackTrace();   }                                          }}


0 0
原创粉丝点击