package my;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import java.awt.*;
import javax.swing.*;import java.awt.event.*;
import java.applet.*;
public class my extends Applet implements ActionListener
{
JTextField snotext,snametext,sagetext,ssextext;
JButton queryButton,clearButton;
public void main()
{
new Object();
}
public void init()
{
JLabel snoLabel=new JLabel("学号:");
JLabel snameLabel=new JLabel("姓名:");
JLabel sageLabel=new JLabel("年龄:");
JLabel ssexLabel=new JLabel("性别:");
snotext=new JTextField(15);
snametext=new JTextField(15);
sagetext=new JTextField(15);
ssextext=new JTextField(15);
queryButton=new JButton("添加");
clearButton=new JButton("清除");
setLayout(null);
snoLabel.setBounds(10, 27, 90, 24); snotext.setBounds(106, 24, 176, 30);
snameLabel.setBounds(10, 70, 90, 24); snametext.setBounds(106, 67, 176, 30);
sageLabel.setBounds(10, 112, 90, 24); sagetext.setBounds(106, 112, 176, 30);
ssexLabel.setBounds(10, 156, 90, 24); ssextext.setBounds(106, 156, 176, 30);
queryButton.setBounds(10, 230, 114, 34); clearButton.setBounds(208, 230, 114, 34);
add(snoLabel); add(snotext);
add(snameLabel); add(snametext);
add(sageLabel); add(sagetext);
add(ssexLabel); add(ssextext);
add(queryButton);add(clearButton);
queryButton.addActionListener(this);
clearButton.addActionListener(this);
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=java.jdbc_test";
String userName="sa";
String userPwd="123456";
try
{ Class.forName(driverName);
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
PreparedStatement stmt=dbConn.prepareStatement("insert into t_user(userName,userPwd) values(?,?,?,?)");
stmt.setString(1,snotext.getText());
stmt.setString(2,snametext.getText());
stmt.setString(3,sagetext.getText());
stmt.setString(4,ssextext.getText());
}
catch(Exception e)
{ e.printStackTrace();System.out.println("失败!"); }
//finally
//{ ConnectionPool.close(stmt,conn); }
}
public void actionPerformed(ActionEvent e)
{
Float sno_gettext=Float.parseFloat(snotext.getText());
if(e.getSource()==queryButton)
{
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=java.jdbc_test";
String userName="sa";
String userPwd="123456";
try
{ Class.forName(driverName);
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
PreparedStatement stmt=dbConn.prepareStatement("insert into t_user(userName,userPwd) values(?,?,?,?)");
stmt.setString(1,snotext.getText());
stmt.setString(2,snametext.getText());
stmt.setString(3,sagetext.getText());
stmt.setString(4,ssextext.getText());
}
catch(Exception e1)
{ e1.printStackTrace();System.out.println("失败!"); }
finally
{ ConnectionPool.close(stmt,conn); }
}
//getSource得到的组件的名称,而getActionCommand得到的是标签。
//resultTF.setText(String.valueOf(result)); [String.toString];
if(e.getSource()==clearButton)
{snotext.setText("");snametext.setText(null);
sagetext.setText(null);ssextext.setText(null);}
}
}
一找不到或无法加载主类,二插入语句思路混乱,三final语句一直报错。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- CSDN专家-sinJack 2021-06-14 07:48关注
main方法中,new Object();改为new my();
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报