java连接SQL数据连接问题
package conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbcfile
{
//申明变量
Connection conn;
Statement stmt;
int inorupdatevalue = -1;
//申明构造方法并抛出异常
public Jdbcfile() throws Exception
{
try
{
String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc.sqlserver://localhost:1433;DatabaseName = library";
Class.forName(drivername);
conn = DriverManager.getConnection(dbURL,"sa","123");////////////////////////(1)
stmt = conn.createStatement();
}catch(ClassNotFoundException e){
//捕获异常
throw new Exception("数据驱动未找到" + e.getException());
}catch(SQLException e){
//捕获异常
throw new Exception("数据库未连接" + e.getMessage());
}
}
//定义查询数据的方法
public synchronized ResultSet executeQuery(String sql)throws Exception{
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
//定义插入数据的方法
public synchronized int insert(String sql)throws Exception{
inorupdatevalue = stmt.executeUpdate(sql);
return inorupdatevalue;
}
//定义删除数据的方法
public synchronized int del(String sql)throws Exception{
inorupdatevalue = stmt.executeUpdate(sql);
return inorupdatevalue;
}
//定义关闭数据库连接的方法
public void close()throws Exception{
conn.close();
}
}
package admin;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import conn.Jdbcfile;
public class Login extends JFrame
{
//声明标签、按钮、文本框密码框
private JLabel JLb1;
private JLabel JLb2;
private JButton OK_btn;
private JButton Cancel_btn;
private JTextField jtflduser;
private JPasswordField jtpwdfld;
//声明窗口
private JFrame frame;
//构造方法
public Login()
{
frame = new JFrame("登入");
Container content = frame.getContentPane();
//采用GridLayout布局管理器
content.setLayout(new GridLayout(3,2,20,20));
JLb1 = new JLabel("用户名");
JLb2 = new JLabel("密 码");
//将标签置于居中位置
JLb1.setHorizontalAlignment(SwingConstants.CENTER);
JLb2.setHorizontalAlignment(SwingConstants.CENTER);
jtflduser = new JTextField();
jtpwdfld = new JPasswordField();
OK_btn = new JButton("确定");
Cancel_btn = new JButton("取消");
//为按钮添加事件监听者
OK_btn.addActionListener(new ActionHandler());
Cancel_btn.addActionListener(new ActionHandler());
//添加标签、文本框、密码框和按钮到窗口
content.add(JLb1);
content.add(jtflduser);
content.add(JLb2);
content.add(jtpwdfld);
content.add(OK_btn);
content.add(Cancel_btn);
frame.pack();
//设定登入窗口启动时出现在屏幕中央位置
frame.setLocationRelativeTo(null);
frame.setSize(300,200);
frame.setVisible(true);
}//Login() method
/**
* 实现ActionListener监听,激活组件响应
*/
class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str1,str2,sqlStr;
Object obj = e.getSource();
//获得文本框和密码框的数据
str1 = jtflduser.getText().trim();
str2 = new String(jtpwdfld.getPassword()).trim();
try{
//单击[确定]按钮
if(obj.equals(OK_btn)){
if(obj.equals(""))
{
JOptionPane.showMessageDialog(frame,"用户不能为空!");
return;
}
//创建数据库连接
Jdbcfile conn = new Jdbcfile();
//产生登入SQL语句
sqlStr = "select * from Admin where num = '" + str1 + "'and password = '" + str2 + "'";
ResultSet result = conn.executeQuery(sqlStr);
if(result.next())
{
//弹出对话框提示登入成功
JOptionPane.showMessageDialog(frame,"登入成功!");
//打开图书管理主页面
bookmain bookmain1 = new bookmain();
bookmain1.go();
//关闭登入窗口
frame.dispose();
//关闭连接数据库
conn.close();
}else
{
JOptionPane.showMessageDialog(frame,"用户名或密码错误");
}
}else if(obj.equals(Cancel_btn)){
//单击【取消】按钮
System.exit(0);
}
}catch(ClassNotFoundException ce){
//捕获异常
System.out.print("SQLException:" + ce.getMessage());
}catch(SQLException ex){
//捕获异常
System.out.print(ex);
}catch(Exception s){
//捕获异常
s.printStackTrace();
}
}
}//class ActionHandler
public static void main(String args[])
{
Login login = new Login();
}//main
}
java.lang.Exception: 数据库未连接No suitable driver found for jdbc.sqlserver://localhost:1433;DatabaseName = library
at conn.Jdbcfile.<init>(Jdbcfile.java:44)
at admin.Login$ActionHandler.actionPerformed(Login.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
小弟是新手请大哥大姐帮帮忙?
[解决办法]
如需要阅读该回复,请登录或注册CSDN!
如需要阅读该回复,请登录或注册CSDN!
如需要阅读该回复,请登录或注册CSDN!