前台的JAVA的GUI登入界面如何连接到数据库?
- 提问者网友:蓝莓格格巫
- 2021-06-05 07:03
- 五星知识达人网友:西岸风
- 2021-06-05 08:19
用的access数据库
public class OLEDB
{
String strDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String strUrl = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=lee.mdb";
String strUserName = "";
String strPassWord = "";
Connection conn;
Statement stmt;
ResultSet rs;
public OLEDB()
{
try
{
Class.forName(strDriver);
conn = DriverManager.getConnection(strUrl, strUserName, strPassWord);
stmt = conn.createStatement();
}
catch (ClassNotFoundException e)
{
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public ResultSet select( String strSQL )
{
try
{
rs = stmt.executeQuery(strSQL);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public void insert( String strSQL )
{
try
{
stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(null,"插入成功!");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void update( String strSQL )
{
try
{
stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(null,"修改成功!");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void delete( String strSQL )
{
try
{
stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(null,"删除成功!");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}