首页 > 代码库 > java-connect-mysql
java-connect-mysql
package connectmysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class Connectsql { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; //驱动路径 String url = "jdbc:mysql://localhost:3306/test";//数据库地址 String user = "root"; //访问数据库的用户名 String password = "123456"; //用户密码 //数据删除 /*try{ String name = "tqt"; int i; Class.forName(driver); //加载驱动 Connection con = DriverManager.getConnection(url, user, password); if(!con.isClosed()){ System.out.println("链接数据库成功:"); String sql="delete from user_70 where name=?"; PreparedStatement query =con.prepareStatement(sql); query.setString(1, name); i=query.executeUpdate(); //影响的行数 System.out.println(i); } } catch (Exception e){ e.printStackTrace(); }*/ //数据修改 /*try{ String name = "tqt"; String address = "sichuan"; String pwd = "54188"; int i; Class.forName(driver); //加载驱动 Connection con = DriverManager.getConnection(url, user, password); if(!con.isClosed()){ System.out.println("链接数据库成功:"); String sql="update user_70 set address=?, password=? where name=?"; PreparedStatement query =con.prepareStatement(sql); query.setString(1, address); query.setString(2, pwd); query.setString(3, name); i=query.executeUpdate(); //影响的行数 System.out.println(i); } } catch (Exception e){ e.printStackTrace(); }*/ //数据插入 /*try{ String name = "tqt"; String address = "mianyang"; String pwd = "081200"; int i; Class.forName(driver); //加载驱动 Connection con = DriverManager.getConnection(url, user, password); if(!con.isClosed()){ System.out.println("链接数据库成功:"); String sql="insert into user_70(name, address, password)values(?, ?, ?)"; PreparedStatement query =con.prepareStatement(sql); query.setString(1, name); query.setString(2, address); query.setString(3, pwd); i=query.executeUpdate(); //影响的行数 System.out.println(i); } } catch (Exception e){ e.printStackTrace(); }*/ //数据查询 /*try{ Class.forName(driver); //加载驱动 Connection con = DriverManager.getConnection(url, user, password); if(!con.isClosed()){ System.out.println("链接数据库成功:"); Statement st = con.createStatement(); //创建Statement对象 String sql="select * from user_70"; ResultSet rs = st.executeQuery(sql); //查询之后返回结果集 while(rs.next()){ System.out.println(rs.getString("name")+"\t"+rs.getString("address")+"\t"+rs.getString("password")); } rs.close(); } con.close(); } catch (Exception e){ e.printStackTrace(); }*/ } }
java-connect-mysql
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。