import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JdbcTest007 {
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection("jdbc:mysql//127.0.0.1:3306/rja2101 ?serverTimezone=UTC","root","123456");
System.out.println(conn);
PreparedStatement pstmt =(PreparedStatement) conn.prepareStatement("select * from/zs");
ResultSet resultSet = pstmt.executeQuery();
while (resultSet.next()) {
String id =resultSet.getString("xh");
String sname =resultSet.getString("xm");
String ssex =resultSet.getString("sex");
String sbirth =resultSet.getNString("birth");
String sdep =resultSet.getString("dep");
System.out.println(id+""+sname+""+ssex+""+sbirth+""+sdep);
}
resultSet.close();
pstmt.close();
conn.close();
}
}
另外一段为import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Add extends JFrame {
JLabel jlxh=new JLabel("学号");
JLabel jlxm=new JLabel("姓名");
JLabel jlsex=new JLabel("性别");
JLabel jlbirth=new JLabel("出生日期");
JLabel jldep=new JLabel("院系");
JTextField jtxh=new JTextField("",20);
JTextField jtxm=new JTextField("",20);
JTextField jtsex=new JTextField("",20);
JTextField jtbirth=new JTextField("",20);
JTextField jtdep=new JTextField("",20);
JButton buttonadd=new JButton("添加");
JButton buttonreturn=new JButton("返回");
//创建构造窗体的空参方法
public Add() {
JPanel jpxh=new JPanel();
JPanel jpxm=new JPanel();
JPanel jpsex=new JPanel();
JPanel jpbirth=new JPanel();
JPanel jpdep=new JPanel();
JPanel jpforbutton=new JPanel(new GridLayout(1,1));
jpxh.add(jlxh);
jpxh.add(jtxh);
jpxm.add(jlxm);
jpxm.add(jtxm);
jpsex.add(jlsex);
jpsex.add(jtsex);
jpbirth.add(jlbirth);
jpbirth.add(jtbirth);
//院系
jpdep.add(jldep);
jpdep.add(jtdep);
//按钮
jpforbutton.add(buttonadd);
jpforbutton.add(buttonreturn);
//将面板放入窗体
this.add(jpxh);
this.add(jpxm);
this.add(jpsex);
this.add(jpbirth);
this.add(jpdep);
this.add(jpforbutton);
//设置窗体信息
this.setTitle("添加学生信息");
this.setLayout(new GridLayout(7,1));
this.setLocation(400,300);
this.setSize(350, 300);
this.setVisible(true);
buttonadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
//得到连接
Connection conn=null;
try {
conn = DriverManager.getConnection("jdbc:mysql//127.0.0.1:3306/rja2101 ?serverTimezone=UTC","root","123456");
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
//像表中插入数据
String sql="INSERT INTO xs(学号,姓名,性别,出生日期,所属院系) values(?,?,?,?)" ;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.setString(1,jtxh.getText());
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.setString(2,jtxm.getText());
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.setString(3,jtsex.getText());
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.setString(4,jtbirth.getText());
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.setString(5,jtdep.getText());
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
ps.executeUpdate();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println("成功增加!!!");
}
});
//为返回按钮注册监听
buttonreturn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
}
});
}
}
运行报错结为Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc://127.0.0.1:3306/rja2101
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at cn.cswu002.JdbcTest007.main(JdbcTest007.java:11)