java获取数据库数据失败
问题相关代码,请勿粘贴截图
package edu.dali17_2;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import edu.dali17_2.GetDBConnection;
public class Data extends JFrame{
JPanel jp1,jp2;
JLabel jl1;
JButton jb1;
JTextField jtf1,jtf2;
JTextArea jta;
Data(){
init();
this.setLocation(580, 270);
this.setSize(300, 300);
this.setTitle("数据库查询");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
void init() {
//this.setLayout(new FlowLayout());
jp1 =new JPanel();
jl1 = new JLabel("请输入学号:");
jtf1 = new JTextField(10);
jb1 = new JButton("确定");
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Connection con;
Statement sql = null;
ResultSet res;
con = GetDBConnection.connectDB("stu", "root", "123456");
if(con==null) {
System.out.println("连接失败");
return ;
}
String strsql,str;
str = jtf1.getText().trim(); //trim删除字符串左右的空格;
if(str.equals("")) {
strsql = "select * from student";
System.out.println("查询失败");
}
else {
strsql = "select * from student where id="+str; //处理字符型
}
try {
sql = con.createStatement();
res = sql.executeQuery(strsql);
jta.setText("");
while(res.next()) {
int id = res.getInt(1);
String name = res.getString(2);
String sex = res.getString(3);
Date date = res.getDate(4);
jta.append(String.valueOf(id)+" "+name+" "+sex+" "+String.valueOf(date)+"\n");
//jta.append(res.getString(1)+" "+res.getString(2)+" "+res.getString(3)+" "+res.getString(4)+"\n");
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
jta = new JTextArea(6,20);
jp1.add(jl1);
jp1.add(jtf1);
jp1.add(jb1);
jp1.add(jta);
this.add(jp1);
}
}
package edu.dali17_2;
import java.sql.*;
public class GetDBConnection {
public static Connection connectDB(String DBName,String id,String p) {
Connection con = null;
String uri =
"jdbc:mysql://localhost:3306/"+DBName+"?useSSL=true&characterENcoding=utf-8";
try { Class.forName("com.sql.jdbc.DRiver");
}
catch(Exception e) {}
try {
con = DriverManager.getConnection(uri,id,p);
}
catch(SQLException e) {}
return con;
}
}
package edu.dali17_2;
public class DataTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Data DB = new Data();
}
}
运行结果及报错内容
连接失败
查询失败
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "con" is null
at edu.dali17_2.Data$1.actionPerformed(Data.java:57)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
我想要达到的结果
究竟是哪里出错了,来个会的人,指导指导。