📄 query1.java
字号:
//查询goods数据库
import java.sql.*;
public class Query1 {
public static boolean select(String name)
{//查询店主所拥有的商品
Connection conn = null;
String username="root";
String password="111111";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("Success loading Mysql Driver!");
}
catch(Exception e){
System.out.println("Error loading Mysql Driver!");
}
try{
conn = DriverManager.getConnection(url,username,password);
// System.out.println("Success connect Mysql Database!");
}
catch(SQLException e){
System.out.println(e.getMessage());
}
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from goods where name='"+name+"'");//对数据库的查询
while (rs.next())
{
return true;
}
//conn.commit();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (null != conn)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
public static boolean select1(String goodsname)
{//查询商品是否存在
Connection conn = null;
String username="root";
String password="111111";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("Success loading Mysql Driver!");
}
catch(Exception e){
System.out.println("Error loading Mysql Driver!");
}
try{
conn = DriverManager.getConnection(url,username,password);
// System.out.println("Success connect Mysql Database!");
}
catch(SQLException e){
System.out.println(e.getMessage());
}
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from goods where goodsname='"+goodsname+"'");//对数据库的查询
while (rs.next())
{
return true;
}
//conn.commit();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (null != conn)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -