标签:pass imp ddr static tsql cts div sel class
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(); }*/ } }
标签:pass imp ddr static tsql cts div sel class
原文地址:http://www.cnblogs.com/tqt--0812/p/6884504.html