当前位置: 技术问答>java相关
我都快没信心了,一个连接MySQL的程序!!!
来源: 互联网 发布时间:2015-10-05
本文导语: 程序为mysql提供的一个程序DateTest.java,代码复制如下: package testsuite; import java.sql.*; import java.util.*; public class DateTest { //~ Methods ............................................................... /** * DOCUMENT...
程序为mysql提供的一个程序DateTest.java,代码复制如下:
package testsuite;
import java.sql.*;
import java.util.*;
public class DateTest
{
//~ Methods ...............................................................
/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
*/
public static void main(String[] args)
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test");
conn.createStatement().executeUpdate(
"create table if not exists calendar_event " +
"(calendar_event_id int not null auto_increment primary key, " +
"start_date datetime not null) type = MyISAM;");
String sql = "insert into calendar_event (start_date) values (?)";
PreparedStatement pst = conn.prepareStatement(sql);
long startDate = 1017684000000L;
System.out.println("About to insert date " + startDate);
pst.setTimestamp(1, new Timestamp(startDate));
pst.executeUpdate();
pst.close();
sql = "select LAST_INSERT_ID()";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
rs.next();
int eventID = rs.getInt(1);
System.out.println("Inserted row " + eventID);
sql = "select start_date from calendar_event where calendar_event_id = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, eventID);
rs = pst.executeQuery();
rs.next();
Timestamp ts = rs.getTimestamp("start_date");
System.out.println("Selected date from row " + eventID + ": " +
ts.getTime());
pst.close();
conn.createStatement().executeUpdate(
"create table if not exists date_test " +
"(yr year) type = MyISAM;");
conn.createStatement().executeUpdate("insert into date_test values (74)");
conn.createStatement().executeUpdate("insert into date_test values (1974)");
rs = conn.createStatement().executeQuery("select * from date_test");
while (rs.next()) {
System.out.println(rs.getDate(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的classpath设置如下:
classpath=.;d:j2sdklib;d:j2sdkjrelib;d:jdbc;d:jdbcmysql-connector-java-2.0.14-bin.jar;
其中d:jdbc目录包含了mySQl提供的两个子目录com和org;
java DateTest出现如下错误:
java.lang.NoClassDefFoundError: DateTest (wrong name: testsuite/DateTest)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
Exception in thread "main"
但若我在jb中运行,则什么错误都没有,帮帮我!!!
package testsuite;
import java.sql.*;
import java.util.*;
public class DateTest
{
//~ Methods ...............................................................
/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
*/
public static void main(String[] args)
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test");
conn.createStatement().executeUpdate(
"create table if not exists calendar_event " +
"(calendar_event_id int not null auto_increment primary key, " +
"start_date datetime not null) type = MyISAM;");
String sql = "insert into calendar_event (start_date) values (?)";
PreparedStatement pst = conn.prepareStatement(sql);
long startDate = 1017684000000L;
System.out.println("About to insert date " + startDate);
pst.setTimestamp(1, new Timestamp(startDate));
pst.executeUpdate();
pst.close();
sql = "select LAST_INSERT_ID()";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
rs.next();
int eventID = rs.getInt(1);
System.out.println("Inserted row " + eventID);
sql = "select start_date from calendar_event where calendar_event_id = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, eventID);
rs = pst.executeQuery();
rs.next();
Timestamp ts = rs.getTimestamp("start_date");
System.out.println("Selected date from row " + eventID + ": " +
ts.getTime());
pst.close();
conn.createStatement().executeUpdate(
"create table if not exists date_test " +
"(yr year) type = MyISAM;");
conn.createStatement().executeUpdate("insert into date_test values (74)");
conn.createStatement().executeUpdate("insert into date_test values (1974)");
rs = conn.createStatement().executeQuery("select * from date_test");
while (rs.next()) {
System.out.println(rs.getDate(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的classpath设置如下:
classpath=.;d:j2sdklib;d:j2sdkjrelib;d:jdbc;d:jdbcmysql-connector-java-2.0.14-bin.jar;
其中d:jdbc目录包含了mySQl提供的两个子目录com和org;
java DateTest出现如下错误:
java.lang.NoClassDefFoundError: DateTest (wrong name: testsuite/DateTest)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
Exception in thread "main"
但若我在jb中运行,则什么错误都没有,帮帮我!!!
|
兄弟你要看书学习了,不过我还是帮你先解决问题。
我假设文件目录:
c:jappDateTest.java
c:japptestsuiteDateTest.class
现在你的当前目录是c:japp
c:japp> java testsuite.DateTest
这样就可以运行
我假设文件目录:
c:jappDateTest.java
c:japptestsuiteDateTest.class
现在你的当前目录是c:japp
c:japp> java testsuite.DateTest
这样就可以运行
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。