首先是用java在mysql中增加数据
import java.sql.* public class jdbc{ public static void main (String[] args){ String driver = "org.git.mm.mysql.Driver"; String url = ‘jdbc:mysql://localhost:3306/my‘; String username = "root"; String password = "123456"; String sql = "insert into users (username,password) values(?,?)"; try{ Class.forName(driver); Connection conn = DriverManager.getConnection(url,username,password); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,"bobuke"); ps.setString(2,"123456"); ps.executeUpdate(); ps.close(); ps.close(); } catch (ClassNotFoundException e){ e.printStackTrace(); } catch (SQLException e){ e.printStackTrace(); } } }
下面是在数据库中查找数据
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; public class Demo { static String selectsql = null; static ResultSet retsult = null; public static final String url = "jdbc:mysql://127.0.0.1/test"; public static final String name = "com.mysql.jdbc.Driver"; public static final String user = "root"; public static final String password = "123456"; public static Connection conn = null; public static PreparedStatement pst = null; public static void main(String[] args) { int paraCount = 5; //读取参数数量 selectsql = "select * from testpass";//SQL语句 try { Class.forName(name);//指定连接类型 conn = DriverManager.getConnection(url, user, password);//获取连接 pst = conn.prepareStatement(selectsql);//准备执行语句 } catch (Exception e) { e.printStackTrace(); } String [] paras = new String [paraCount]; try { retsult = pst.executeQuery();//执行语句,得到结果集 while (retsult.next()) { for(int i = 0;i<paraCount;i++){ paras[i] = retsult.getString(i+2); } System.out.println(Arrays.toString(paras)); //显示数据 } retsult.close(); conn.close();//关闭连接 pst.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public class Demo { //这是对上面的代码进行改进 public static final String url = "jdbc:mysql://127.0.0.1/test"; public static final String driver = "com.mysql.jdbc.Driver"; public static final String user = "root"; public static final String password = "123456"; public static void main(String[] args) { int paraCount = 5; //读取参数数量 String selectsql = "select * from testpass";//SQL语句 try { Class.forName(driver);//指定连接类型 Connection conn = DriverManager.getConnection(url, user, password);//获取连接 PreparedStatement pst = conn.prepareStatement(selectsql);//准备执行语句 } catch (Exception e) { e.printStackTrace(); } String [] paras = new String [paraCount]; try { ResultSet retsult = pst.executeQuery();//执行语句,得到结果集 while (retsult.next()) { for(int i = 0;i<paraCount;i++){ paras[i] = retsult.getString(i+2); //+2表示留出空格 } System.out.println(Arrays.toString(paras)); //显示数据 } retsult.close(); conn.close();//关闭连接 pst.close(); } catch (SQLException e) { e.printStackTrace(); } } }
while(result.next()) //上面的while循环部分也可以按照这样的形式来写 { System.out.print(result.getString("id"));//输出对应表内容,“表的列名” System.out.print(" "+result.getString("name")); System.out.println(" "+result.getString("age")); }
时间: 2024-10-12 17:32:44